Access dictionary key’s element by index


The program creates a dictionary student with five key-value pairs. The list function is used to convert the dictionary to a list. The list contains tuples of key-value pairs in the same order as they were in the original dictionary. The print function is used to print the first to fifth elements (i.e., the keys) of the list. The elements are accessed using square brackets and indices, starting from 0.

In summary, the program demonstrates how to convert a dictionary to a list in Python and how to access individual elements in the list. However, it's important to note that the order of elements in a dictionary is not guaranteed, so accessing elements in a dictionary by index is not a good practice. It's better to access elements in a dictionary by their keys.

Source Code

student = {"Name": "Pooja", "Age":23, "Gender": "Female", "City": "Salem", "Mark":488}
print(list(student)[0])
print(list(student)[1])
print(list(student)[2])
print(list(student)[3])
print(list(student)[4])

Output

Name
Age
Gender
City
Mark