I have this:
lst = [2,2,3,3]
c1 = Counter(lst)
x = c1.values()
I want to know why I get this:
x = dict_values([2, 2])
and what can i do to get this:
x = [2,2]
I want to do this so I can manipulate and compare the data inside, the only way I have found is by doing
x = []
for i in c1.values():
x.append(i)
but I was looking for a more direct way to do this, like
x = c1.values()
I tried looking on the net but I can’t find anything
>Solution :
You can turn any iterable (including a dict_values) into a list by passing it to the builtin list constructor:
x = list(c1.values())
Never use builtin names like list or dict as variable names; if you do, like you did here:
list = [2,2,3,3]
you won’t be able to use the builtin list any more because you’ve overwritten its name. You’ll need to edit your code to change the name of that variable before trying to use list(c1.values()).