0

I want to extract the values from a dictionary and print them as a list. For example: If i have letter = {"i": 3, "o": 2, "u": 2}

and want to extract 3,2, and 2 and print it as a list

[3, 2, 2] How do I do this? I've tried

print([x[::] for x in letter])

However, this prints out ['i', 'o', 'u'] and not [3, 2, 2]. Thank you for the help in advanced :)

0

3 Answers 3

3

There is a method in Python called .keys() that allows you to get the keys from a dict. Similarly, there is also a method called values() for the converse.

These are dict instance methods, ie:

myDict = { "i": 0, "t": 1, "f": 2 } print(myDict.values()) 
Sign up to request clarification or add additional context in comments.

Comments

0

You can just call dict.values() Further details here

3 Comments

when I use dict.values(letter) it returns dict_values([3, 2, 2]) however i want it to just return [3, 2, 2]. How do i do this?
list(dict.values())
@Zooby: If you're using a recent version of Python you can also do [*letter.values()]. Bear in mind that a plain dict is an unordered collection, so the ordering of the values list may not be what you expect it to be.
0

Try letter.values(), which gives you dict_values([3, 2, 2])

>>> letter = {"i": 3, "o": 2, "u": 2} >>> print(list(letter.values())) [3, 2, 2] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.