A
A
Arbil2020-06-16 16:54:03
Python
Arbil, 2020-06-16 16:54:03

How to access a dictionary element by index?

How to access a dictionary element by index?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Kuts, 2020-06-16
@Arbil

Well, if you use the latest versions of python where the dictionaries are ordered, and if you completely stop:
my_dict = {'a': 11, 'b': 22, 'c': 33}

>>> my_dict[list(my_dict.keys())[0]]
11
>>> my_dict[list(my_dict.keys())[2]]
33
>>> my_dict[list(my_dict.keys())[1]]
22

But something doesn’t occur to me - in what real cases it may be needed

A
alexusbon, 2020-06-16
@lexa_lashak

Dictionaries in Python are unordered collections of arbitrary objects accessed by key . They are sometimes also called associative arrays...

A
AlexBoss, 2020-06-16
@AlexBoss

collections.OrderedDict
You can also throw it into enumerate later. But are you sure there is no other option?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question