Skip to content
Related Articles

Related Articles

Python – Combine list with other list elements

Improve Article
Save Article
  • Last Updated : 26 Oct, 2022
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')]

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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!