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
# Python3 code to demonstrate working of # Convert list elements to bi-tuples # using zip() + repeat() method from itertools import repeat # initialize list test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ] # printing original list print ( "The original list is : " + str (test_list)) # initialize attach element ele = 'gfg' # Convert list elements to bi-tuples # using zip() + repeat() res = list ( zip (test_list, repeat(ele))) # printing result print ( "Tuple list after attaching element : " + str (res)) |
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
# Python3 code to demonstrate working of # Convert list elements to bi-tuples # using map() + lambda from itertools import repeat # initialize list test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ] # printing original list print ( "The original list is : " + str (test_list)) # initialize attach element ele = 'gfg' # Converting list elements to bi-tuples # using map() + lambda res = list ( map ( lambda key: (key, ele), test_list)) # printing result print ( "Tuple list after attaching element : " + str (res)) |
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
# Python3 code to demonstrate working of # Convert list elements to bi-tuples # initialize list test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ] # printing original list print ( "The original list is : " + str (test_list)) # initialize attach element ele = 'gfg' res = [] # Convert list elements to bi-tuples for i in test_list: res.append((i, ele)) # printing result print ( "Tuple list after attaching element : " + str (res)) |
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
# Python3 code to demonstrate working of # Convert list elements to bi-tuples # using list comprehension # initialize list test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ] # printing original list print ( "The original list is : " + str (test_list)) # initialize attach element ele = 'gfg' # Convert list elements to bi-tuples # using list comprehension res = [(i, ele) for i in test_list] # printing result print ( "Tuple list after attaching element : " + str (res)) # This code is contributed by Edula Vinay Kumar Reddy |
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
# Import the 'cycle' function from the 'itertools' module from itertools import cycle # Initialize a list of values test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ] # printing original list print ( "The original list is : " + str (test_list)) # Create a list containing the attach element ele = "gfg" # Use the 'zip' function to attach the element # to each value in the list # The 'cycle' function is used to repeat the attach element # as many times as needed res = list ( zip (test_list, cycle([ele]))) # Print the resulting list of tuples print ( "Tuple list after attaching element : " + str (res)) |
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
# Python3 code to demonstrate working of # Convert list elements to bi-tuples # using for loop # initialize list test_list = [ 5 , 6 , 7 , 8 , 4 , 3 ] # printing original list print ( "The original list is : " + str (test_list)) # initialize attach element ele = 'gfg' # Convert list elements to bi-tuples # using for loop res = [] for i in test_list: tup = (i, ele) res.append(tup) # printing result print ( "Tuple list after attaching element : " + str (res)) |
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)