Python: Check multiple keys exists in a dictionary
Python dictionary: Exercise-33 with Solution
Write a Python program to check if multiple keys exist in a dictionary.
Visual Presentation:
Sample Solution:
Python Code:
# Create a dictionary 'student' with key-value pairs representing a student's information.
student = {
'name': 'Alex',
'class': 'V',
'roll_id': '2'
}
# Check if the set of keys in 'student' contains the keys 'class' and 'name'.
print(student.keys() >= {'class', 'name'})
# Check if the set of keys in 'student' contains the keys 'name' and 'Alex'.
# Note that 'Alex' is not a key, so this check will always be False.
print(student.keys() >= {'name', 'Alex'})
# Check if the set of keys in 'student' contains the keys 'roll_id' and 'name'.
print(student.keys() >= {'roll_id', 'name'})
Sample Output:
True False True
Python Code Editor:
Previous: Write a Python program to print a dictionary line by line.
Next: Write a Python program to count number of items in a dictionary value that is a list.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/python-exercises/dictionary/python-data-type-dictionary-exercise-33.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics