Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

accessing values() in collection.Counter as a list

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading