Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Find dictionary keys Present in a Strings List
A dictionary is one of Python's core data types consisting of key-value pairs. A strings list contains elements represented as strings. Python provides several built-in functions like keys(), set(), intersection(), and append() to find dictionary keys that are present in a strings list.
Let's take an example:
- Given dictionary:
{'T': 1, 'M': 2, 'E': 3, 'C': 4} - Given list:
['T', 'A', 'B', 'P', 'E', 'L'] - Result:
['T', 'E']
Key Functions Used
keys() Returns all keys from a dictionary as a view object.
set() Creates a set object to store unique elements and perform set operations.
intersection() Finds common elements between two sets.
append() Adds an element to the end of a list.
Using List Comprehension
List comprehension provides a concise way to iterate through dictionary keys and check if they exist in the string list ?
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("Dictionary keys present in strings list:", res)
Dictionary keys present in strings list: ['Tablets', 'Earbuds']
Using Set and Intersection
This method uses set operations to find common elements between dictionary keys and the string list ?
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("Dictionary keys present in strings list:", res)
Dictionary keys present in strings list: ['Projector']
Using For Loop
A traditional approach using a for loop to iterate through dictionary keys and check membership ?
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("Dictionary keys present in strings list:", res)
Dictionary keys present in strings list: ['A', 'C', 'E']
Using Filter Function
The filter() function with a lambda expression provides a functional programming approach ?
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("Dictionary keys present in strings list:", res)
Dictionary keys present in strings list: ['mno']
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
| List Comprehension | High | Good | Simple, readable code |
| Set Intersection | Medium | Best | Large datasets |
| For Loop | High | Good | Step-by-step logic |
| Filter Function | Medium | Good | Functional programming style |
Conclusion
Python offers multiple approaches to find dictionary keys present in a strings list. Use set intersection for best performance with large datasets, list comprehension for readable code, and for loops when you need explicit control over the iteration process.
