Python: how to convert a dictionary into a subscriptable array?

Go To StackoverFlow.com

10

I know how to convert a dictionary into an list in Python, but somehow when I try to get, say, the sum of the resulting list, I get the error 'dict_values' object is not subscriptable. Also, I plan to sum only several items in the list.

dict = {A:1, B:2, C:3, D:4}
arr = dict.values()
the_sum = sum(arr[1:3])

Upon closer inspection, I noticed that when the resulting list is printed out, it always gives dict_values(......) as the output which I can't remove. How do I get around this?

2015-11-12 14:41
by quotable7
You should be super careful here ... As written, there is no way to know which values you're going to get from this expression. e.g. the answer could be 3, 4, 5, 6 or 7 (and it could change from one run to the next) since you have no way to be guaranteed which are the second and third values because dict are unordered - mgilson 2015-11-12 15:05
In my actual dictionary, the values are already written into it in order. So wouldn't converting it into a list ensure everything is in order - quotable7 2015-11-12 15:10
No. It (mostly) doesn't matter what order you write elements into the dict -- They can come back out in a completely different order. If you want to preserver order, make sure you use a collections.OrderedDict - mgilson 2015-11-12 15:11
Ok, thanks for that - quotable7 2015-11-12 15:24


20

In python-3.X dict.values doesn't return a list like how it performs in python-2.X. In python-3.x it returns a dict-value object which is a set-like object and uses a hash table for storing its items. This feature, in addition to supporting most of set attributes, is very optimized for some operations like membership checking (using in operator). And because of that, it doesn't support indexing.

If you want to get a list object, you need to convert it to list by passing the result to the list() function.

the_values = dict.values()
SUM = sum(list(the_values)[1:10])
2015-11-12 14:45
by Kasrâmvd
it's giving error TypeError: 'dict_values' object is not callable - Vishnu Upadhyay 2015-11-12 14:50
@VishnuUpadhyay Did you used values=dict.values()? I mentioned that don't use built-in names as your variable names - Kasrâmvd 2015-11-12 14:50
That works fine for me.. - quotable7 2015-11-12 14:54


3

By assigning dict.values() to list u are not converting it to list u are just storing it as dict_values (may be an object). To convert write list=list(dict.values()). Now even while printing the list u will get the list elements and not dict_values(......).

And as Mentioned before don't use python built-in names as your variable names, it may cause conflicts during execution and confusions while reading your code.

2015-11-12 14:56
by Devansh Bansal


1

Yes, it did appear like a list on the older Python questions asked here. But as @Kasramvd said, assuming you are using python 3.X, dict.values is a dictionary view object. (Also, you definitely came up with this example hastily as you have four dictionary entries but want 10 list items, which is redundant.)

2015-11-12 14:51
by txsaw1
Thanks for pointing out what the dictionary view object is, I think it just allows you to "see" what's in the dictionary - quotable7 2015-11-12 14:54


0

@mgilson is right. A dictionary , by its intrinsic nature, isn't ordered.

Still you can do this :

alpha = {"A":1,"B":2,"C":3,"D":4,"E":5}   # dictionary
my_list = []
for key,value in alpha.items() :
    my_list.append(value)

You can access your "values" from my_list, but it will not be in order.

2017-06-22 11:43
by sharad jain
Ads