Python Forum
How to iterate dict_values data type - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to iterate dict_values data type (/thread-25954.html)



How to iterate dict_values data type - nisusavi - Apr-16-2020

Hi All,
Noob to python here. I am trying to iterate and capture/store the respective ask values for 740.0 and 750.0 in the the example below. So far no luck. Any help is much appreciated. Is dict_values different from dict. Based on my research, it is new with Python 3 and is an iterator and not a dictionary.

Trimmed variable output below to keep it simple

Output:
>>> print(callStrikes_dict) dict_values([{'740.0': [{'putCall': 'CALL', ... 'bid': 12.75, 'ask': 13.4, 'last': 13.3, ....}], '750.0': [{'putCall': 'CALL', ... 'bid': 8.75, 'ask': 9.4, 'last': 10.3, ....,'mini': False}]}]) >>> type(callStrikes_dict) <class '[b]dict_values[/b]'>



RE: How to iterate dict_values data type - bowlofred - Apr-16-2020

You can iterate over it directly

for value in callStrikes_dict:
    print(f"One value is {value}")
or you could coerce it into a list if it's easier to deal with that way. But the list is a copy of the values at that point in time. While the dict.values() object will see changes to the dictionary. Either way may be preferred.

list_of_values = list(callStrikes_dict)



RE: How to iterate dict_values data type - nisusavi - Apr-17-2020

Thanks bowlofred for your response. This one worked

next(iter(callStrikes_dict))['740.0']