Python | Convert list elements to bi-tuples
Last Updated :
24 Apr, 2023
Sometimes, while working with Python lists, we can have a subproblem in which we need to tie a particular element with each of the list elements. This task can have its utility in the web development domain while working with queries and filters. Let’s discuss ways to solve this problem.
Method 1: Using zip() + repeat() This problem can be solved using zip() which can be used to attach to each list element with a decided number using repeat().
Python3
from itertools import repeat
test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ]
print ( "The original list is : " + str (test_list))
ele = 'gfg'
res = list ( zip (test_list, repeat(ele)))
print ( "Tuple list after attaching element : " + str (res))
|
Output
The original list is : [5, 6, 7, 8, 4, 3]
Tuple list after attaching element : [(5, 'gfg'), (6, 'gfg'), (7, 'gfg'), (8, 'gfg'), (4, 'gfg'), (3, 'gfg')]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using map() + lambda This is yet another way in which this task can be performed. In this, we just extend the logic of adding element to list element to all elements using map(), by feeding it with lambda function to perform computation tasks.
Python3
from itertools import repeat
test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ]
print ( "The original list is : " + str (test_list))
ele = 'gfg'
res = list ( map ( lambda key: (key, ele), test_list))
print ( "Tuple list after attaching element : " + str (res))
|
Output
The original list is : [5, 6, 7, 8, 4, 3]
Tuple list after attaching element : [(5, 'gfg'), (6, 'gfg'), (7, 'gfg'), (8, 'gfg'), (4, 'gfg'), (3, 'gfg')]
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n), as the list of bi-tuples is created which takes up n elements.
Method #3: Using append() method
Python3
test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ]
print ( "The original list is : " + str (test_list))
ele = 'gfg'
res = []
for i in test_list:
res.append((i, ele))
print ( "Tuple list after attaching element : " + str (res))
|
Output
The original list is : [5, 6, 7, 8, 4, 3]
Tuple list after attaching element : [(5, 'gfg'), (6, 'gfg'), (7, 'gfg'), (8, 'gfg'), (4, 'gfg'), (3, 'gfg')]
Time complexity: O(n), where n is the length of the input list ‘test_list’. This is because the for loop iterates through all the elements of the list once.
Auxiliary space: O(n), where n is the length of the input list ‘test_list’.
Method #4: Using list comprehension
This approach uses list comprehension to iterate over the elements of the input list and attach the element ele to each of them, creating a new list of tuples.
Example:
Python3
test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ]
print ( "The original list is : " + str (test_list))
ele = 'gfg'
res = [(i, ele) for i in test_list]
print ( "Tuple list after attaching element : " + str (res))
|
Output
The original list is : [5, 6, 7, 8, 4, 3]
Tuple list after attaching element : [(5, 'gfg'), (6, 'gfg'), (7, 'gfg'), (8, 'gfg'), (4, 'gfg'), (3, 'gfg')]
Time complexity: O(N) – the time it takes to iterate through the entire list once and create a new tuple for each element.
Auxiliary space: O(N) – the space used to create the new list of tuples, which is equal to the size of the original list.
Method #5: Using itertools cycle:
Python3
from itertools import cycle
test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ]
print ( "The original list is : " + str (test_list))
ele = "gfg"
res = list ( zip (test_list, cycle([ele])))
print ( "Tuple list after attaching element : " + str (res))
|
Output
The original list is : [5, 6, 7, 8, 4, 3]
Tuple list after attaching element : [(5, 'gfg'), (6, 'gfg'), (7, 'gfg'), (8, 'gfg'), (4, 'gfg'), (3, 'gfg')]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: using a for loop
Approach:
- Initialize an empty list to store the resulting bi-tuples.
- Loop through each element of the original list.
- Create a bi-tuple using the current element and the predefined element ‘gfg’.
- Append the bi-tuple to the result list.
- Print the resulting list.
Example:
Python3
test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ]
print ( "The original list is : " + str (test_list))
ele = 'gfg'
res = []
for i in test_list:
tup = (i, ele)
res.append(tup)
print ( "Tuple list after attaching element : " + str (res))
|
Output
The original list is : [5, 6, 7, 8, 4, 3]
Tuple list after attaching element : [(5, 'gfg'), (6, 'gfg'), (7, 'gfg'), (8, 'gfg'), (4, 'gfg'), (3, 'gfg')]
Time Complexity: O(n) (where n is the length of the list)
Auxiliary Space: O(n) (for the result list)
Share your thoughts in the comments
Please Login to comment...