I'm not sure why I am struggling to find this answer, but I am. I want to print the third key and item from a Python dictionary. I don't want to for loop and print all values like many other questions seem to ask. Simply, I just want dictionaryData.keys()[2]
. I know this is incorrect, but this is what I am trying to accomplish.
What is the correct way to obtain the third key from a dictionary?
CodePudding user response:
i think Carl HR's answer is the easiest and straight forward way
CodePudding user response:
To print the third key and its associated value you could do this:
d = {1:'A',2:'B',3:'C'}
print(*list(d.items())[2])
Output:
3 C
Note:
This assumes that there at least 3 key/value pairs in the dictionary
Or:
If you just want to be weird then:
_, _, (k, v), *_ = d.items()
print(k, v)