Skip to content
Related Articles

Related Articles

Python – Tuple List intersection (Order irrespective)

View Discussion
Improve Article
Save Article
  • Last Updated : 08 Oct, 2021

Given list of tuples, perform tuple intersection of elements irrespective of their order.

Input : test_list1 = [(3, 4), (5, 6)], test_list2 = [(5, 4), (4, 3)] 
Output : {(3, 4)} 
Explanation : (3, 4) and (4, 3) are common, hence intersection ( order irrespective).

Input : test_list1 = [(3, 4), (5, 6)], test_list2 = [(5, 4), (4, 5)] 
Output : set() 
Explanation : No intersecting element present. 
 

Method #1 : Using sorted() + set() + & operator + list comprehension 
The combination of above functions can be used to solve this problem. In this, we sort the tuples, and perform intersection using & operator. 

Python3




# Python3 code to demonstrate working of
# Tuple List intersection [ Order irrespective ]
# Using sorted() + set() + & operator + list comprehension
 
# initializing lists
test_list1 = [(3, 4), (5, 6), (9, 10), (4, 5)]
test_list2 = [(5, 4), (3, 4), (6, 5), (9, 11)]
 
# printing original list
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Using sorted() + set() + & operator + list comprehension
# Using & operator to intersect, sorting before performing intersection
res = set([tuple(sorted(ele)) for ele in test_list1]) & set([tuple(sorted(ele)) for ele in test_list2])
 
# printing result
print("List after intersection : " + str(res))

Output : 

The original list 1 is : [(3, 4), (5, 6), (9, 10), (4, 5)]
The original list 2 is : [(5, 4), (3, 4), (6, 5), (9, 11)]
List after intersection : {(4, 5), (3, 4), (5, 6)}

 

Method #2 : Using list comprehension + map() + frozenset() + & operator 
The combination of above functions can be used to perform this task. In this, we perform the task of conversion of innercontainers to sets, which orders it, and performs the intersection. Frozenset is used as its hashable, and map() requires hashable data type as argument.

Python3




# Python3 code to demonstrate working of
# Tuple List intersection [ Order irrespective ]
# Using list comprehension + map() + frozenset() + & operator
 
# initializing lists
test_list1 = [(3, 4), (5, 6), (9, 10), (4, 5)]
test_list2 = [(5, 4), (3, 4), (6, 5), (9, 11)]
 
# printing original list
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Using list comprehension + map() + frozenset() + & operator
# frozenset used as map() requires hashable container, which
# set is not, result in frozenset format
res = set(map(frozenset, test_list1)) & set(map(frozenset, test_list2))
 
# printing result
print("List after intersection : " + str(res))

Output : 

The original list 1 is : [(3, 4), (5, 6), (9, 10), (4, 5)]
The original list 2 is : [(5, 4), (3, 4), (6, 5), (9, 11)]
List after intersection : {frozenset({4, 5}), frozenset({5, 6}), frozenset({3, 4})}

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!