![]() |
VOOZH | about |
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.
The original list is : ['GeeksforGeeks is best for geeks', 'I love GeeksforGeeks'] The matching keys list : [['best', 'Geeks'], ['love', 'Geeks']]
Time complexity: O(NMK), where N is the length of the input list 'test_list', M is the maximum length of a string in the 'test_list', and K is the number of keys in the 'test_dict'. This is because the program uses a list comprehension nested inside another list comprehension to iterate over each string in the input list and then iterate over each key in the dictionary to find matches.
Auxiliary space: O(N*M), where N is the length of the input list 'test_list' and M is the maximum length of a string in the 'test_list'. This is because the program creates a new list 'res' of the same size as 'test_list' to store the results.
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.
The original list is : ['GeeksforGeeks is best for geeks', 'I love GeeksforGeeks'] The matching keys list : [['best', 'Geeks'], ['love', 'Geeks']]
Time complexity: O(n * m).
Auxiliary space: O(n * m).
Method #3: Using for loop
This program works by iterating through each string in the test_list, and then iterating through each key in the test_dict. If the key is found in the string, it is added to a list of keys for that string. Finally, the list of keys for each string is added to the res list.
The matching keys list : [['Geeks', 'best'], ['Geeks', 'love']]
Time complexity: O(n*m*k), where n is the length of test_list, m is the number of keys in test_dict, and k is the average length of each string in test_list.
Auxiliary space: O(n*k), where n is the length of test_list and k is the maximum number of keys that can be present in a single string in test_list.
Method #4: Using the map() function
This method uses a lambda function with a list comprehension to map the dictionary keys to each string in the list using the map() function. The list() function is then used to convert the map object to a list.
Step-by-step approach:
The matching keys list: [['Geeks', 'best'], ['Geeks', 'love']]
Time complexity: O(nm), where n is the length of the list and m is the length of the dictionary.
Auxiliary space: O(nm) as well, since a new list is created for each string in the list.
Method #5: Using Pandas
Step-by-step approach:
Output:
The matching keys list : [['Geeks', 'best'], ['Geeks', 'love']]
Time complexity: O(nkm), where n is the number of strings in test_list, k is the number of keys in test_dict, and m is the average length of a string in test_list.
Auxiliary space: O(n*k), because we store the list of matching keys for each string in the df1 dataframe.