Python – Find dictionary keys present in a Strings List

Sometimes, while working with Python dictionaries, we can have problem in which we need to perform the extraction of dictionary keys from strings list feeded. This problem can have application in many domains including data. Lets discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension
This is brute force way in which this task can be performed. In this, we use conditional statements to find the strings matching keys in each string and extract keys from string list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract dictionary keys in Strings List
# Using list comprehension
  
# initializing list
test_list = ["GeeksforGeeks is best for geeks", "I love GeeksforGeeks"]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing dictionary
test_dict = {'Geeks' : 5, 'CS' : 6, 'best' : 7, 'love' : 10}
  
# Extract dictionary keys in Strings List
# Using list comprehension
res = [ele if len(ele) > 0 else [None] for ele in [[key for key in test_dict if key in sub]
                                       for sub in test_list]]
                                         
# printing result 
print("The matching keys list : " + str(res)) 

chevron_right


Output :

The original list is : ['GeeksforGeeks is best for geeks', 'I love GeeksforGeeks']
The matching keys list : [['best', 'Geeks'], ['love', 'Geeks']]

 

Method #2 : Using filter() + lambda + list comprehension
The combination of above functions can also be used to solve this problem. In this, we perform the task of filtering using filter() and lambda and list comprehension help to reduce one level of nesting.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract dictionary keys in Strings List
# Using filter() + lambda + list comprehension
  
# initializing list
test_list = ["GeeksforGeeks is best for geeks", "I love GeeksforGeeks"]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing dictionary
test_dict = {'Geeks' : 5, 'CS' : 6, 'best' : 7, 'love' : 10}
  
# Extract dictionary keys in Strings List
# Using filter() + lambda + list comprehension
res = [list(filter(lambda ele: ele in sub, test_dict)) for sub in test_list]
                                         
# printing result 
print("The matching keys list : " + str(res)) 

chevron_right


Output :

The original list is : ['GeeksforGeeks is best for geeks', 'I love GeeksforGeeks']
The matching keys list : [['best', 'Geeks'], ['love', 'Geeks']]



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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.