Reputation: 13172
I want to access a specific element of a tuple in a dictionary of tuples. Let's say that I have a dictionary with a unique key, and a tuple with three values, for each key. I want to write a iterator the prints every third item in a tuple for every element in the dictionary.
For example
dict = {"abc":(1,2,3), "bcd":(2,3,4), "cde", (3,4,5)}
for item in dict:
print item[2]
But this returns
c
d
e
Where am I going wrong?
Upvotes: 5
Views: 21570
Reputation: 66
Your code is close, but you must key into the dictionary and THEN print the value in index 2.
You are printing parts of the Keys. You want to print parts of the Values associated with those Keys:
for item in dict:
print dict[item][2]
Upvotes: 2
Reputation: 17552
for item in dict:
print dict[item][2]
Also, you should not name anything after a built-in, so name your dictionary 'd'
or something other than 'dict'
for item in dict:
does the same thing as for item in dict.keys()
.
Alternatively, you can do:
for item in dict.values():
print item[2]
Upvotes: 8