Python – Extract dictionary items with List elements

Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract all the items from dictionary that constitute all the elements from a list. This kind of problem can occur in domains such as competitive programming and web development. Let’s discuss certain ways in which this task can be performed.

Input : test_dict = {‘gfg’ : [4, 6, 10], ‘is’ : [12]}
Output : {‘gfg’: [4, 6, 10]}

Input : test_dict = {‘gfg’ : [4, 6, 10]}
Output : {‘gfg’: [4, 6, 10]}

Method #1 : Using set() + dictionary comprehension + items()
The combination of above functionalities can be used to solve this problem. In this, we first reduce the list elements to remove duplicate, extract dictionary items using items() and construction of required dictionary happens using dictionary comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract dictionary items with List elements
# Using set() + dictionary comprehension + items()
  
# helpr_fnc
def hlper_fnc(req_list, test_dict):
    temp = set(req_list)
    temp2 = { key: set(val) for key, val in test_dict.items() }
    return { key: val for key, val in test_dict.items() if temp2[key].issubset(temp)}
  
# initializing dictionary
test_dict = {'gfg' : [4, 6], 'is' : [10], 'best' : [4, 5, 7]}
  
# printing original dictionary
print("The original dictionary : " + str(test_dict))
  
# initializing req_list 
req_list = [4, 6, 10]
  
# Extract dictionary items with List elements
# Using set() + dictionary comprehension + items()
res = hlper_fnc(req_list, test_dict)
      
# printing result 
print("The extracted dictionary: " + str(res)) 

chevron_right


Output :



The original dictionary : {‘is’: [10], ‘best’: [4, 5, 7], ‘gfg’: [4, 6]}
The extracted dictionary: {‘is’: [10], ‘gfg’: [4, 6]}

 

Method #2 : Using all() + dictionary comprehension
The combination of above functions can be used to solve this problem. This is one-liner solution and uses all() to check for elements membership to decide for filteration. The dictionary comprehension performs task of construction of dictionary.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract dictionary items with List elements
# Using all() + dictionary comprehension
  
# initializing dictionary
test_dict = {'gfg' : [4, 6], 'is' : [10], 'best' : [4, 5, 7]}
  
# printing original dictionary
print("The original dictionary : " + str(test_dict))
  
# initializing req_list 
req_list = [4, 6, 10]
  
# Extract dictionary items with List elements
# Using all() + dictionary comprehension
res = {key: val for key, val in test_dict.items() if all(vals in req_list for vals in val)}
      
# printing result 
print("The extracted dictionary: " + str(res)) 

chevron_right


Output :

The original dictionary : {'is': [10], 'best': [4, 5, 7], 'gfg': [4, 6]}
The extracted dictionary: {'is': [10], 'gfg': [4, 6]}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.