Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dict.values() doesn't return a list as expected

I'm 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

So I should get something like ['a', 'b', 'etc']. However in my example I get:

type(d.values()) ---> <class 'dict_values'>

What's wrong?

like image 476
nachtblume Avatar asked Apr 19 '26 12:04

nachtblume


1 Answers

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

like image 187
kosciej16 Avatar answered Apr 22 '26 08:04

kosciej16



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!