Getting the key value by position from the dictionary

I use the python dictionary tel = {'jack': 4098, 'sape': 4139 ....., 'john': 5147} Is there a way to get all the values ​​located between the index ranges? for example, all values ​​located from 5th to 10th position ...

Thanks Nathan

+3
source share
2 answers

There are no indexes / positions in the python dictionary. The key / value pair does not have a specific order.

If you want to select certain positions from the dictionary, you must indicate some criteria / order to it.

For example, according to the alphabetical order of names:

sorted(tel.items())[5:11]

returns a list of 5 tuples (name, number)from positions 5-10 in alphabetical order.

+6
source

Python . Python . keys, . Ordered Dict, .OrderedDict, , , OrderedDict odict, , .

+3

Source: https://habr.com/ru/post/1788447/


All Articles