Compare Dictionary with List in Python (2 Examples)
In this tutorial, you’ll learn how to compare a dictionary with a list in Python. Comparing a dictionary with a list involves checking if certain elements exist in both data structures and determining their common elements. We’ll explore different methods to achieve this comparison.
The table of contents is structured as follows:
Let’s get started!
Initializing Sample Dictionary and List
Firstly, let’s initialize a sample dictionary and a sample list:
# Initializing sample dictionary my_dict = {'a': 1, 'b': 2, 'c': 3} # Initializing sample list my_list = [1, 2, 3]
This creates a dictionary, my_dict
, with key-value pairs, and a list, my_list
, with some elements.
Example 1: Checking If Dictionary Keys are in List
One way to compare a dictionary with a list is to check if the keys of the dictionary exist in the list. Here’s an example:
# Checking if keys are in my_list common_keys = [key for key in my_dict if key in my_list]
In this example, we use a list comprehension to iterate over the keys of the dictionary and check if each key exists in the list. The keys that are common to both the dictionary and the list are stored in the common_keys
list.
Let’s print the common keys:
print(common_keys) # []
The output shows that the keys of my_dict
are not included in my_list
.
Example 2: Checking If Dictionary Values are in List
Another way to compare a dictionary with a list is to check if the values of the dictionary exist in the list. Here’s how you can do it:
# Checking if values in my_list common_values = [value for value in my_dict.values() if value in my_list]
In this example, we use a list comprehension to iterate over the values of the dictionary and check if each value exists in the list. The values that are common to both the dictionary and the list are stored in the common_values
list.
Let’s print the common values:
print(common_values) # [1, 2, 3]
The output shows the values that exist in both the dictionary and the list.
Video, Further Resources & Summary
In this tutorial, we explored different methods to compare a dictionary with a list in Python. We learned how to check the existence of keys in the dictionary and how to check the existence of values in the list. These techniques can be helpful in scenarios where you need to find common elements between a dictionary and a list.
The YouTube video will be added soon.
For more Python programming tutorials and examples, you can check out the following resources on Statistics Globe:
- Iterate Over Nested Dictionary with List in Python (Example)
- Introduction to List in Python
- Convert Dictionary to List in Python (3 Examples)
- Convert List to Dictionary with Index as Values in Python (2 Examples)
- Python Programming Language for Statistics & Data Science
- Merge Sort Dictionary in Python (Example)
Now you have the knowledge and techniques to compare a dictionary with a list in Python. Happy coding!
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.
Statistics Globe Newsletter