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

dict.values() doesnt return list as expected

Im trying to get values from my dictionary here

for elem in set(sample_txt):

       d = {elem:sample_txt.count(elem)}

print(d.values())

d.values() should return a list of values:

The methods dict. keys() and dict. values() return lists of the keys
or values explicitly. There’s also an items() which returns a list of
(key, value) tuples, which is the most efficient way to examine all
the key value data in the dictionary.
from developers.google.com

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

So i should get something like this [‘a’,’b’, ‘etc’]. However in my example i get

type(d.values()) —> <class ‘dict_values’>

What’s wrong

>Solution :

The quote you posted is related to python2, where indeed it returned list. In python3 you need to cast it by yourself list(d.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