Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Combine list with other list elements

Improve Article
Save Article
  • Last Updated : 08 Mar, 2023
Improve Article
Save Article

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')]

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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!