Python - Find dictionary keys Present in a Strings List


The dictionary is one of the famous datatypes of Python that consist of keys with value pairs. The strings list is the set of elements that are represented either in a single or double quotation. In Python, we have some built-in functions such as keys(), set(), intersection, and append() will be used to find the dictionary keys present in strings List.

Let’s take an example of this.

There are two available sets:-

The given dictionary, {'T': 1, 'M': 2, 'E': 3, 'C': 4}

The given list, [T, A, B, P, E, L]

Therefore, the final result becomes [T, E].

Syntax

The following syntax is used in the examples

keys()

The keys() is an in-built function in Python that returns the list of all available keys from the dictionary.

set()

The set() is an in-built function in Python that can be used to store multiple elements into a single variable.

intersection()

The built-in function intersection() of Python finds the common match from the two different sets.

append()

The append() is an in-built function in Python that inserts the elements at the end of the list.

Using List Comprehension

In the following example, the program starts with a recursive function named dict_keys() that accepts two parameters- d and s to fetch the value from the given input dictionary and string i.e. dict1 and stng. Next, it will use the list comprehension technique where first it set the key as variable initialization. Using this variable the loop iterate through the dictionary to check the condition using an if-statement i.e. whether the string is available in the given dictionary or not and returns the result.

Example

def dict_keys(d, s):
    return [key for key in d if key in s]
# The input dictionary and string list
dict1 = {'Tablets': 1, 'Mobiles': 2, 'Earbuds': 3, 'Computer': 4}
stng = ['Mouse', 'Hard Drive', 'Pen Drive', 'Earbuds', 'Tablets']
# Calling function
res = dict_keys(dict1, stng)
print("The result of dictionary keys present in a strings list:\n", res)

Output

 The result of dictionary keys present in a strings list:
 ['Tablets', 'Earbuds']

Using Set() and Intersection() Function

In the following example, the recursive function named dict_keys() accepts two parameters i.e. d and s to find the dictionary keys present in the list. Next, it will use the function return to set some built-in function such as d.keys() that show the view object of the dictionary whereas intersection(s) find the common match between two different sets. At last both these functions are set to the parameter of the built-in function list() that will return the result in the form of a list.

Example

def dict_keys(d, s):
    return list(set(d.keys()).intersection(s))
# Create the dictionary and string list
dictionary = {'Television': 1, 'Projector': 2, 'White Board': 3}
strings = ['Projector', 'Box', 'Remote']
# Calling function
res = dict_keys(dictionary, strings)
print("The result of dictionary keys present in a strings list:\n", res)

Output

 The result of dictionary keys present in a strings list: 
['Projector']

Using Simple for Loop

In the following example, there will be two parameters- d_list and s_list in the function named dict_keys that will find the dictionary keys present in a string list. Then use the empty list that can be used to store the result in the form of a list and store it in the variable avl_keys. Next, using for loop, the variable k iterates through the variable d_list i.e. dictionary list. Using an if-statement the condition is whether the string present in the given input dictionary. Then use the built-in method append() that finds the dictionary keys and returns to the variable avl_keys and generates the result.

Example

def dict_keys(d_list, s_list):
    avl_keys = []
    for k in d_list:
        if k in s_list:
            avl_keys.append(k)
    return avl_keys
# Create the dictionary and string list
dict1 = {'A': 11, 'B': 12, 'C': 13, 'D': 14, 'E': 15, 'F': 16}
stng = ['M', 'N', 'O', 'E', 'G', 'A', 'C', 'P', 'Q', 'R']
# Calling function
res = dict_keys(dict1, stng)
print("The result of dictionary keys present in a strings list:\n", res)

Output

 The result of dictionary keys present in a strings list: 
 ['A', 'C', 'E']

Using filter Function

In the following example, we will start the program with a recursive function that accepts two parameters- d and s to receive the original input of dictionary and string from the variable dcn and stng. Next, it will use the function return that uses some built-in function lambda- calculate the main operation of the program, filter()- get those value which are present in the dictionary, and list()- to format the result in the form a list. Finally, this way it will generate the output of the program.

Example

def dict_keys(d, s):
    return list(filter(lambda k: k in s, d))
# Create the dictionary and string list
dcn = {'abc': 10, 'bcd': 20, 'mno': 30, 'xyz': 40, 'pqr': 50}
stng = ['ab', 'mno', 'bcs', 'xy']
# Calling Function
res = dict_keys(dcn, stng)
print("The result of dictionary keys present in a strings list:\n", res)

Output

 The result of dictionary keys present in a strings list: 
['mno']

Conclusion

In every example, the dictionary and strings are used to set the original input of the program where it is set for operations i.e. keys present in the strings list. There are various built-in functions used such as filter(), set(), keys(), etc to fulfilling the special need when required.

Updated on: 16-Aug-2023

183 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements