Python Forum
How to iterate dict_values data type
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to iterate dict_values data type
#1
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]'>
Reply
#2
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)
Reply
#3
Thanks bowlofred for your response. This one worked

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,261 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  accessing value in dict_values class CompleteNewb 14 5,021 Mar-31-2022, 04:02 AM
Last Post: deanhystad
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 1,911 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 4,255 Jan-30-2021, 07:11 AM
Last Post: alloydog
  What is the meaning of mutable data type? qliu 3 2,876 Apr-17-2020, 07:20 PM
Last Post: deanhystad
  Data Type conversion error rajeevjagatap 2 4,269 Apr-15-2020, 03:29 PM
Last Post: rajeevjagatap
  Type hinting - return type based on parameter micseydel 2 2,425 Jan-14-2020, 01:20 AM
Last Post: micseydel
  iterate read data from excel sheet jp2017 1 2,303 Jun-19-2019, 07:45 PM
Last Post: micseydel
  Reading in MYSQL data as integer type Daveinthebigcity 2 3,480 May-13-2019, 11:30 AM
Last Post: Daveinthebigcity
  Type error when reading in different data types on an __init__ method Dylanmull 3 2,748 May-09-2019, 02:05 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020