In python when I have a dictionary (e.g. data
), I can get the dictionary keys by using the keys method, i.e. keys = data.keys()
.
If I try to access the fist key as in a list, i.e. keys[0]
or as an iterator fashion, i.e. next(keys)
I will get the following exception: TypeError: 'dict_keys' object is not an iterator
.
However I can iterate over the dictionary keys using a for loop, e.g. keys_list = [item for item in keys]
. Does not this mean that keys
is an iterator?
What am I missing?
Accessing the keys of an instance of a python dictionary object