Skip to content
Related Articles

Related Articles

Python – All possible pairs in List

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 03 Jul, 2020
View Discussion
Improve Article
Save Article

Sometimes, while working with Python list, we can have a problem in which we need to extract all the possible pairs that can be performed from integers from list. This kind of problem can occur in many domains such as day-day programming and web development. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [1, 7, 4]
Output : [(1, 7), (1, 4), (7, 4)]

Input : test_list = [7, 4]
Output : [(7, 4)]

Method #1 : Using list comprehension + enumerate()
This is one of the ways in which this task can be performed. In this, we perform the task of pairing using nested loops in list comprehension recipe, and enumerate() is used to check with the next indices while iteration.




# Python3 code to demonstrate working of 
# All possible pairs in List
# Using list comprehension + enumerate()
  
# initializing list
test_list = [1, 7, 4, 3]
  
# printing original list 
print("The original list : " + str(test_list))
  
# All possible pairs in List
# Using list comprehension + enumerate()
res = [(a, b) for idx, a in enumerate(test_list) for b in test_list[idx + 1:]]
  
# printing result 
print("All possible pairs : " + str(res))

Output :

The original list : [1, 7, 4, 3]
All possible pairs : [(1, 7), (1, 4), (1, 3), (7, 4), (7, 3), (4, 3)]

 

Method #2 : Using combinations()
This is one of the ways in which this task can be performed. In this, we just using inbuild function for pairing and sending 2 as value for making pairs of size 2.




# Python3 code to demonstrate working of 
# All possible pairs in List
# Using combinations()
from itertools import combinations
  
# initializing list
test_list = [1, 7, 4, 3]
  
# printing original list 
print("The original list : " + str(test_list))
  
# All possible pairs in List
# Using combinations()
res = list(combinations(test_list, 2))
  
# printing result 
print("All possible pairs : " + str(res))

Output :

The original list : [1, 7, 4, 3]
All possible pairs : [(1, 7), (1, 4), (1, 3), (7, 4), (7, 3), (4, 3)]

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!