Open In App

Python – Combine list with other list elements

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Combine list with other list elements
# Using zip() + len() + list()
 
# initializing list
test_list = [3, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing pair list
pair_list = ['Gfg', 'is', 'best']
 
# using zip() to pair element with pair list size
res = list(zip([test_list] * len(pair_list), pair_list))
 
# printing result
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




# Python3 code to demonstrate working of
# Combine list with other list elements
# Using product()
from itertools import product
 
# initializing list
test_list = [3, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing pair list
pair_list = ['Gfg', 'is', 'best']
 
# product() performs pairing of elements
res = list(product([test_list], pair_list))
 
# printing result
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




# Python3 code to demonstrate working of
# Combine list with other list elements
 
# initializing list
test_list = [3, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing pair 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)
# printing result
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:

  1. Define two lists, test_list and pair_list.
  2. Use map() and lambda to create a list of tuples where each tuple contains the entire test_list and one element from pair_list.
  3. Convert the resulting map object to a list and store it in the variable res.
  4. Print the original list and the paired list.

Python3




# define the test_list and pair_list
test_list = [3, 5, 7, 9]
pair_list = ['Gfg', 'is', 'best']
 
# use map() and lambda to create a list of tuples where each tuple contains the entire test_list and one element from pair_list
res = list(map(lambda x: (test_list, x), pair_list))
 
# printing original list and paired 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():

  1. Initialize the original list test_list and print it.
  2. Initialize the pair list pair_list.
  3. 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.
  4. 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.
  5. Print the resulting paired list res.

Python3




# Python3 code to demonstrate working of
# Combine list with other list elements
# Using reduce()
 
# import reduce from functools module
from functools import reduce
 
# initializing list
test_list = [3, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing pair list
pair_list = ['Gfg', 'is', 'best']
 
# define lambda function to combine elements
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]
 
# apply lambda function to the list using reduce method
res = reduce(combine, [test_list], [])
 
# printing result
print("The paired list : " + str(res))
#This code is contributed by Jyothi pinjala.


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
Previous
Next
Share your thoughts in the comments
Similar Reads