Python – Common list elements and dictionary values
Given list and dictionary, extract common elements of List and Dictionary Values.
Input : test_list = [“Gfg”, “is”, “Best”, “For”], subs_dict = {4 : “Gfg”, 8 : “Geeks”, 9 : ” Good”, }
Output : [‘Gfg’]
Explanation : “Gfg” is common in both list and dictionary value.
Input : test_list = [“Gfg”, “is”, “Best”, “For”, “Geeks”], subs_dict = {4 : “Gfg”, 8 : “Geeks”, 9 : ” Best”, }
Output : [‘Gfg’, “Geeks”, “Best”]
Explanation : 3 common values are extracted.
Method #1 : Using list comprehension + values()
The combination of above functionalities provide a way in which this task can be performed in a single line. In this, we extract values of dictionaries using values() and list comprehension is used to perform iteration and intersection checks.
Python3
# Python3 code to demonstrate working of# List elements and dictionary values intersection# Using list comprehension + values()# initializing listtest_list = ["Gfg", "is", "Best", "For", "Geeks"]# printing original listprint("The original list : " + str(test_list))# initializing subs. Dictionarysubs_dict = {4 : "Gfg", 8 : "Geeks", 9 : " Good", }# Intersection of elements, using "in" for checking presenceres = [ele for ele in test_list if ele in subs_dict.values()] # printing resultprint("Intersection elements : " + str(res)) |
The original list : ['Gfg', 'is', 'Best', 'For', 'Geeks'] Intersection elements : ['Gfg', 'Geeks']
Method #2 : Using set() + intersection()
In this approach both list and dictionary values are converted to set() and then intersection is performed to get common elements.
Python3
# Python3 code to demonstrate working of# Common list elements and dictionary values# Using set() and intersection()# initializing listtest_list = ["Gfg", "is", "Best", "For", "Geeks"]# printing original listprint("The original list : " + str(test_list))# initializing subs. Dictionarysubs_dict = {4 : "Gfg", 8 : "Geeks", 9 : " Good", }# Intersection of elements, using set() to convert# intersection() for common elementsres = list(set(test_list).intersection(list(subs_dict.values()))) # printing resultprint("Intersection elements : " + str(res)) |
The original list : ['Gfg', 'is', 'Best', 'For', 'Geeks'] Intersection elements : ['Geeks', 'Gfg']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


