Python Forum
accessing value in dict_values class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
accessing value in dict_values class
#15
Reading through this one last time I think I might know the question.
Quote:How do I get the values from a dict_values object?
As stevendprano pointed out, dict.values() returns a view object. You can read about them here:

https://docs.python.org/3.2/library/stdt...ew-objects

As pointed out in his post, a view object has several interesting attributes. The most interesting to me is that it is dynamic. If you change the dictionary after making the view, the view reflects the changes. So a view is not a collection, but rather a "view" into another collection.
x = {1:1, 2:4, 3:9}
y = x.values()
print(y)
x[2] = 42
print(y)
Output:
dict_values([1, 4, 9]) dict_values([1, 42, 9])
According to the documentation a view object can be iterated over. You can use a view object in a for loop, in a comprehension, and pass it as an argument to list(). So if you want to treat a views object like it was a list, the easiest way is use list() to make a list.
x = {1:1, 2:4, 3:9}
y = list(x.values())
print(y, y[0])
Output:
[1, 4, 9] 1
I still don't understand how the values will be used, but I can leave that question unanswered without too much discomfort. The pairs, triples and quadruples are all red herring. Now that noob knows how to get the values from the dictionary view he'll figure out how to use them.
Reply


Messages In This Thread
RE: accessing value in dict_values class - by deanhystad - Mar-31-2022, 04:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,400 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  How to iterate dict_values data type nisusavi 2 7,790 Apr-17-2020, 08:06 PM
Last Post: nisusavi
  accessing local variable outside class priyanka08 3 2,265 Sep-24-2019, 10:00 AM
Last Post: buran
  Global accessing of parameter to class Mahamutha 3 3,627 Aug-23-2017, 07:04 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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