Python – Combine list with other list elements
Given two lists, combine list with each element of the other list.
Examples:
Input : test_list = [3, 5, 7], pair_list = [‘Gfg’, ‘is’, ‘best’]
Output : [([3, 5, 7], ‘Gfg’), ([3, 5, 7], ‘is’), ([3, 5, 7], ‘best’)]
Explanation : All lists paired with each element from other list.
Input : test_list = [3, 5, 7], pair_list = [‘Gfg’, ‘best’]
Output : [([3, 5, 7], ‘Gfg’), ([3, 5, 7], ‘best’)]
Explanation : All lists paired with each element from other list.
Method #1 : Using zip() + len() + list()
In this, we pair each element using zip(), with all the elements of other list using len(), and picking each element at once.
Python3
test_list = [ 3 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
pair_list = [ 'Gfg' , 'is' , 'best' ]
res = list ( zip ([test_list] * len (pair_list), pair_list))
print ( "The paired list : " + str (res))
|
Output
The original list is : [3, 5, 7, 9]
The paired list : [([3, 5, 7, 9], 'Gfg'), ([3, 5, 7, 9], 'is'), ([3, 5, 7, 9], 'best')]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. zip() + len() + list() performs n number of operations.
Auxiliary Space: O(n), extra space of size n is required
Method #2 : Using product()
In this, we pair the elements using product(), and map each list with each element in pair list.
Python3
from itertools import product
test_list = [ 3 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
pair_list = [ 'Gfg' , 'is' , 'best' ]
res = list (product([test_list], pair_list))
print ( "The paired list : " + str (res))
|
Output
The original list is : [3, 5, 7, 9]
The paired list : [([3, 5, 7, 9], 'Gfg'), ([3, 5, 7, 9], 'is'), ([3, 5, 7, 9], 'best')]
Method #3: Using append() and tuple() methods
Python3
test_list = [ 3 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
pair_list = [ 'Gfg' , 'is' , 'best' ]
res = []
for i in pair_list:
x = []
x.append(test_list)
x.append(i)
x = tuple (x)
res.append(x)
print ( "The paired list : " + str (res))
|
Output
The original list is : [3, 5, 7, 9]
The paired list : [([3, 5, 7, 9], 'Gfg'), ([3, 5, 7, 9], 'is'), ([3, 5, 7, 9], 'best')]
Time Complexity: O(n), where n is length of pair_list.
Auxiliary Space: O(n), where n is length of res list to store result.
Method #4: Using map() function and lambda function
Step-by-step algorithm:
- Define two lists, test_list and pair_list.
- Use map() and lambda to create a list of tuples where each tuple contains the entire test_list and one element from pair_list.
- Convert the resulting map object to a list and store it in the variable res.
- Print the original list and the paired list.
Python3
test_list = [ 3 , 5 , 7 , 9 ]
pair_list = [ 'Gfg' , 'is' , 'best' ]
res = list ( map ( lambda x: (test_list, x), pair_list))
print ( "The original list is : " + str (test_list))
print ( "The paired list : " + str (res))
|
Output
The original list is : [3, 5, 7, 9]
The paired list : [([3, 5, 7, 9], 'Gfg'), ([3, 5, 7, 9], 'is'), ([3, 5, 7, 9], 'best')]
Time complexity:
The time complexity of the algorithm is O(n), where n is the length of pair_list. This is because the lambda function is executed for each element in pair_list, and the map() function has a time complexity of O(n).
Auxiliary space:
The auxiliary space complexity of the algorithm is O(n), where n is the length of pair_list. This is because the list res has a space complexity of O(n), which is the same as the space complexity of pair_list.
Method #5: Using reduce():
- Initialize the original list test_list and print it.
- Initialize the pair list pair_list.
- Define the combine lambda function, which takes two arguments acc and elem, and returns a new list of pairs. If acc is non-empty, it pairs each element of pair_list with each element of acc and appends the resulting pairs to acc. If acc is empty, it pairs each element of pair_list with elem and returns the resulting pairs. The lambda function is defined using a ternary operator.
- Apply the reduce method to the list [test_list] using the combine lambda function and an empty list [] as the initial value of the accumulator. The reduce method applies the combine function to the accumulator and the first element of the list, then applies it again to the result and the second element of the list, and so on, until all elements of the list have been processed. The final result is a list of pairs obtained by pairing each element of pair_list with each element of test_list.
- Print the resulting paired list res.
Python3
from functools import reduce
test_list = [ 3 , 5 , 7 , 9 ]
print ( "The original list is : " + str (test_list))
pair_list = [ 'Gfg' , 'is' , 'best' ]
combine = lambda acc, elem: acc + [(elem1, elem2) for elem1 in acc[ - 1 ] for elem2 in pair_list] if acc else [(elem, elem2) for elem2 in pair_list]
res = reduce (combine, [test_list], [])
print ( "The paired list : " + str (res))
|
Output
The original list is : [3, 5, 7, 9]
The paired list : [([3, 5, 7, 9], 'Gfg'), ([3, 5, 7, 9], 'is'), ([3, 5, 7, 9], 'best')]
The time complexity: O(n * m^2), where n is the length of test_list and m is the length of pair_list.
The space complexity : O(n * m^2), due to the list of pairs that is generated as a result.
Last Updated :
21 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...