How do you access the second element of a dictionary in python?

Viewed 1079

I have a dictionary called one, and from it I extracted the first element of the dictionary by doing this:

first1 = one.items()
first = list(first1)[:1]
print(first)

But now I want to print only the second element of the dictionary. If I do this:

first1 = one.items()
first = list(first1)[:2]
print(first)

It prints out both the first element and the second element, but I want only the second one. How can I do this? Is there a special symbol that helps? If you know, please tell me. Thanks.

3 Answers

In your first snippet I think you meant this:

first1 = one.items()
first = list(first1)[0]
print(first)

This then leads to finding the second item like this:

first1 = one.items()
first = list(first1)[1]
print(first)

Don't include a colon:

first1 = one.items()
first = list(first1)[1]
print(first)

The colon means to slice the list, instead try this:

first1 = one.items()
first = list(first1)[1]
print(first)
Related