Python Forum

Full Version: How to iterate dict_values data type
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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]'>
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)
Thanks bowlofred for your response. This one worked

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