Skip to content
Related Articles

Related Articles

Python – Assign pair elements from Tuple Lists

View Discussion
Improve Article
Save Article
  • Last Updated : 30 Aug, 2020

Given a tuple list, assign with each element, its pair elements from other similar pairs.

Input : test_list = [(5, 3), (7, 5), (8, 4)]
Output : {5: [3], 7: [5], 8: [4], 4: []}
Explanation : 1st elements are paired with respective 2nd elements from all tuples.

Input : test_list = [(5, 3)]
Output : {5: [3]}
Explanation : Only one tuples, 5 paired with 3.

Method : Using setdefault() + loop

In this, we use brute way to solve this, iterate for each tuple, and set default values for each, key and value as empty list, appending the elements to respective list if already present.

Python3




# Python3 code to demonstrate working of 
# Assign pair elements from Tuple Lists
# Using setdefault + loop
  
# initializing list
test_list = [(5, 3), (7, 5), (2, 7), (3, 8), (8, 4)]
  
# printing string
print("The original list : " + str(test_list))
  
# initializing dictionary
res = dict()
for key, val in test_list:
      
    # adding to both, corresponding keys and values
    res.setdefault(val, [])
    res.setdefault(key, []).append(val)
  
# printing results 
print("The resultant pairings : " + str(res))


Output

The original list : [(5, 3), (7, 5), (2, 7), (3, 8), (8, 4)]
The resultant pairings : {3: [8], 5: [3], 7: [5], 2: [7], 8: [4], 4: []}
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!