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

后端 未结 4 442
终归单人心
终归单人心 2020-12-09 15:52

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

4条回答
  •  -上瘾入骨i
    2020-12-09 16:32

    By assigning dict.values() to a list you are not converting it to a list; you are just storing it as dict_values (may be an object). To convert it write list=list(dict.values()). Now even while printing the list you 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 confusion while reading your code.

提交回复
热议问题