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
How to Common keys in list and dictionary using Python
In this article, we will learn how to find common keys between a list and dictionary in Python. This is useful when you need to filter dictionary keys based on a list of allowed values.
Methods Used
The following are the various methods to accomplish this task ?
Using the 'in' operator and List Comprehension
Using set() and intersection() functions
Using keys() function & in operator
Using the Counter() function
Example Setup
Assume we have taken an input dictionary and list. We will find the common elements between the input list and keys of a dictionary using the above methods ?
Input
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
inputList = ["hello", "tutorialspoint", "python"]
Expected Output
Resultant list: ['hello', 'tutorialspoint']
In the above example, 'hello' and 'tutorialspoint' are the common elements between the input list and keys of the dictionary.
Using List Comprehension with 'in' Operator
List comprehension provides a concise way to create lists based on existing sequences. The 'in' operator checks membership in a sequence ?
# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
# input list
inputList = ["hello", "tutorialspoint", "python"]
print("Input dictionary:", inputDict)
print("Input list:", inputList)
# checking whether any input list element matches the keys of a dictionary
outputList = [key for key in inputDict if key in inputList]
# printing the resultant list
print("Resultant list:", outputList)
Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: ['hello', 'tutorialspoint']
Using set() and intersection() Functions
The set() function creates a set object that removes duplicates. The intersection() method returns elements that are present in both sets ?
# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
# input list
inputList = ["hello", "tutorialspoint", "python"]
print("Input dictionary:", inputDict)
print("Input list:", inputList)
# getting common elements using set intersection
outputList = list(set(inputList).intersection(set(inputDict.keys())))
# printing the resultant list
print("Resultant list:", outputList)
Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: ['hello', 'tutorialspoint']
Using keys() Function with 'in' Operator
The keys() method provides a view of all dictionary keys. We can iterate through these keys and check membership in the input list ?
# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
# input list
inputList = ["hello", "tutorialspoint", "python"]
print("Input dictionary:", inputDict)
print("Input list:", inputList)
# empty list for storing common elements
outputList = []
# getting the list of keys of a dictionary
keysList = list(inputDict.keys())
# traversing through the keys list
for key in keysList:
# checking whether the current key is present in the input list
if key in inputList:
# appending that key to the output list
outputList.append(key)
# printing the resultant list
print("Resultant list:", outputList)
Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: ['hello', 'tutorialspoint']
Using Counter() Function
The Counter() function from the collections module counts hashable objects. It creates a dictionary with element frequencies ?
# importing Counter function from the collections module
from collections import Counter
# input dictionary
inputDict = {"hello": 2, "all": 4, "welcome": 6, "to": 8, "tutorialspoint": 10}
# input list
inputList = ["hello", "tutorialspoint", "python"]
print("Input dictionary:", inputDict)
print("Input list:", inputList)
# getting the frequency of input list elements as a dictionary
frequency = Counter(inputList)
# empty list for storing common elements
outputList = []
# traversing through dictionary keys
for key in inputDict.keys():
# checking whether the current key is present in the input list
if key in frequency:
# appending that key to the output list
outputList.append(key)
# printing the resultant list
print("Resultant list:", outputList)
Input dictionary: {'hello': 2, 'all': 4, 'welcome': 6, 'to': 8, 'tutorialspoint': 10}
Input list: ['hello', 'tutorialspoint', 'python']
Resultant list: ['hello', 'tutorialspoint']
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| List Comprehension | O(n*m) | Readable, concise code |
| Set Intersection | O(n+m) | Large datasets, fastest |
| keys() with loop | O(n*m) | Step-by-step processing |
| Counter() | O(n+m) | When frequency data is needed |
Conclusion
We explored four methods for finding common keys between a list and dictionary. The set intersection method is most efficient for large datasets, while list comprehension offers the most readable syntax.
