Skip to content
Related Articles

Related Articles

Python – Extract dictionary items with List elements

Improve Article
Save Article
  • Last Updated : 31 Jan, 2022
Improve Article
Save Article

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.
 

Python3




# 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))

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 filtration. The dictionary comprehension performs task of construction of dictionary.
 

Python3




# 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))

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
Related Articles

Start Your Coding Journey Now!