Python's dictionaries have no order, so indexing like you are suggesting (fruits[2]
) makes no sense as you can't retrieve the second element of something that has no order. They are merely sets of key:value
pairs.
To retrieve the value at key
: 'kiwi'
, simply do: fruit['kiwi']
. This is the most fundamental way to access the value of a certain key. See the documentation for further clarification.
And passing that into a print()
call would actually give you an output:
print(fruit['kiwi'])
#2.0
Note how the 2.00
is reduced to 2.0
, this is because superfluous zeroes are removed.
Finally, if you want to use a for-loop
(don't know why you would, they are significantly more inefficient in this case (O(n)
vs O(1)
for straight lookup)) then you can do the following:
for k, v in fruit.items():
if k == 'kiwi':
print(v)
#2.0
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…