0

when working with lists in python, they're indexed allowing you to pull any value via its index. For example:

list = ['a', 'b', 'c', 'd']
print(list[1])

>>> b

However, from what I've seen, this doesn't exist in dictionaries. In order to pull an object you have to type in the actual key itself to do that.

dict = {'a' = a, 'b' = b, 'c' = c}

If I wanted to pull the second, let's say the second key('b') without having to type the key itself, instead using it's index(which would be 1, if indexing exists in dictionaries that is) is there any way to do that? From my research I haven't been able to find a way to do this.

  • 1
    No, there is no simple and efficient way to do this. Also, the syntax for your dictionary is not right for Python - should be colons `:` instead of equal signs `=`. – kaya3 Feb 26 '20 at 23:41
  • 1
    Does this answer your question? [Python: get key of index in dictionary](https://stackoverflow.com/questions/11632905/python-get-key-of-index-in-dictionary) – Ahmed Maher Feb 26 '20 at 23:44
  • whoops, thanks kaya3. My bad, I made this on a whim – Bumbo McPastry Feb 27 '20 at 21:43

1 Answers1

1

Are you looking for something like this:

from collections import OrderedDict

letter_list = ['a', 'b', 'c', 'd']
letter_list = zip(letter_list, letter_list)
letter_dict = OrderedDict(letter_list)
print(letter_dict.items()[1][0])

Output

b