0

Say, I have the following dictionary in Python:

dict = {'YoMama': 'hot', 'temp_min': '100', 'temp_max': '200'}

I know you can access them by their 'key' names but how do you access them with their numbers? [0], 1

If I want to access the second 'key' name (temp_min)

ex:

print(dict.keys()[1])

OR

If I want to access just the first value (hot)

ex:

print(dict.values()[1])

How do you accomplish this please?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
jssteele89
  • 463
  • 1
  • 4
  • 11
  • 1
    Generally, if you want to access the elements of a dictionary *by position* you are using the wrong data structure. The `dict` API provides no way to do it, so it will always requires iterating over it. – juanpa.arrivillaga Aug 04 '22 at 22:18
  • 1
    The first value would be `print(dict.values()[0])` but is not a good idea use a dictionary to access values por position. If you want access data by position use a `list`. Are two different use cases – Gonzalo Odiard Aug 04 '22 at 22:18
  • Perhaps the *least bad* way of doing this, though, is something like `next(itertools.islice(mydict, index, None))` – juanpa.arrivillaga Aug 04 '22 at 22:20
  • @juanpa.arrivillaga I'm pretty sure `islice` iterates through the dictionary to find the i-th element returned by the iterator, so this is not an efficient solution. – kaya3 Aug 04 '22 at 22:22
  • But note, if you are going to do this multiple times, it is best to just create a list up-front and index that. Of course, that list won't reflect any changes to the `dict`. But something like `keys = list(mydict)` or `values = list(mydict.values())` – juanpa.arrivillaga Aug 04 '22 at 22:22
  • @kaya3 I didn't say it was efficient, indeed, I mentioned that iterating over the dict is required. I said that this might be the *least bad* way of doing it since at least it is constant space. To do it efficiently (in constant time) you would need to rely on the internals of the CPython dict implementation, which changes a lot. It would be possible with `ctypes` though. – juanpa.arrivillaga Aug 04 '22 at 22:22
  • @GonzaloOdiard I've tried that command numerous times and it doesn't work FYI. I would post screenshot If I could. Is there a way I can upload one to show you? – jssteele89 Aug 04 '22 at 22:38
  • @jssteele89 ok, you can do `print(list(dict.values())[0])` but is a bad idea anyway as everybody is telling you – Gonzalo Odiard Aug 04 '22 at 23:25

2 Answers2

0

In general, you should not rely on key ordering (if you need this, use a list), but there's nothing special to it. Get the string, then use it

second_key = list(data.keys())[1]  # or list(data)[1]
print(data[second_key])

Or simply print(list(data.values())[1])

(Don't use dict as a variable name)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

The iterables returned by dict.keys, dict.values, and dict.items aren't subscriptable, but you can convert them into lists (which are subscriptable) with the list() constructor:

>>> my_dict = {'YoMama': 'hot', 'temp_min': '100', 'temp_max': '200'}
>>> list(my_dict)
['YoMama', 'temp_min', 'temp_max']
>>> list(my_dict.values())
['hot', '100', '200']
>>> list(my_dict.items())
[('YoMama', 'hot'), ('temp_min', '100'), ('temp_max', '200')]

at which point you can access them by index number.

Note however that if you need to access items by the order, rather than their keys, you probably don't want this to be a dict in the first place! A list of tuples (i.e. the list returned by list(my_dict.items()) might be more appropriate.

Samwise
  • 68,105
  • 3
  • 30
  • 44