w3resource

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:

Python List: Access dictionary key’s element by index.

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:

Flowchart: Get the depth of a dictionary

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.



Follow us on Facebook and Twitter for latest update.