Python: Access dictionary key’s element by index
Python dictionary: Exercise-55 with Solution
Write a Python program to access dictionary key's element by index.
Visual Presentation:
Sample Solution:
Python Code:
# Create a dictionary 'num' with subject names as keys and corresponding marks as values.
num = {'physics': 80, 'math': 90, 'chemistry': 86}
# Print a message indicating the intention to access the keys of the dictionary and retrieve them as a list.
print("\nAccess the keys of the dictionary as a list:")
# Access and print the first key (subject name) in the dictionary.
print(list(num)[0])
# Access and print the second key (subject name) in the dictionary.
print(list(num)[1])
# Access and print the third key (subject name) in the dictionary.
print(list(num)[2])
Sample Output:
Access the keys of the dictionary as a list: physics math chemistry
Flowchart:
Python Code Editor:
Previous: Get the depth of a dictionary.
Next: Convert a given dictionary into a list of lists.
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-55.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics