Python | Find Symmetric Pairs in dictionary
Sometimes, while working with Python dictionary, one can have a problem in which one desires to get key-value pairs that are symmetrical, i.e that has key-value pair of same value irrespective of the fact value is a key or value. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using generator + loop
This task can be solved in brute force method using loops and generators by yielding at runtime the values of matching key-value pairs.
# Python3 code to demonstrate working of # Find Symmetric Pairs in dictionary # using generator + loop # generator function to perform task def find_sym_pairs(test_dict): for key in test_dict.keys(): val = test_dict.get(key) if test_dict.get(val) == key: yield key, val return # Initializing dict test_dict = {'a' : 1, 'b' : 2, 'c' : 3, 1 : 'a', 2 : 'b'} # printing original dict print("The original dict is : " + str(test_dict)) # Find Symmetric Pairs in dictionary # using generator + loop res = [] for key, val in find_sym_pairs(test_dict): temp = (key, val) res.append(temp) # printing result print("The pairs of Symmetric values : " + str(res)) |
The original dict is : {'a': 1, 1: 'a', 'c': 3, 'b': 2, 2: 'b'}
The pairs of Symmetric values : [('a', 1), (1, 'a'), ('b', 2), (2, 'b')]
Method #2 : Using list comprehension
This task can also be performed as a one-liner using list comprehension as a shortened way of performing loop-based solution.
# Python3 code to demonstrate working of # Find Symmetric Pairs in dictionary # Using list comprehension # Initializing dict test_dict = {'a' : 1, 'b' : 2, 'c' : 3, 1 : 'a', 2 : 'b'} # printing original dict print("The original dict is : " + str(test_dict)) # Find Symmetric Pairs in dictionary # Using list comprehension temp = [(key, value) for key, value in test_dict.items()] res = [(x, y) for (x, y) in temp if (y, x) in temp] # printing result print("The pairs of Symmetric values : " + str(res)) |
The original dict is : {'a': 1, 1: 'a', 'c': 3, 'b': 2, 2: 'b'}
The pairs of Symmetric values : [('a', 1), (1, 'a'), ('b', 2), (2, 'b')]
Recommended Posts:
- Python | Get first N key:value pairs in given dictionary
- Python | Find the closest Key in dictionary
- Python | Find depth of a dictionary
- Python program to find second maximum value in Dictionary
- Python program to find the sum of all items in a dictionary
- Python | Find dictionary matching value in list
- Python program to find the highest 3 values in a dictionary
- Dictionary and counter in Python to find winner of election
- Python | Find keys with duplicate values in dictionary
- Find the first repeated word in a string in Python using Dictionary
- Python Dictionary to find mirror characters in a string
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Find common elements in three sorted arrays by dictionary intersection
- Python | Convert string dictionary to dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

