say that the following is our code:
d = {"Happy":"Clam", "Sad":"Panda"}
for i in d:
print(i)
now print(i) will print out just the keys, but how could I change it so that it printed the values?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A Python dict has a number of methods available for getting a list of the keys, values, or both.
To answer your question, you can use
d.values()to get a list of the values only:Output:
The
items()method is particularly useful, though, so should be mentioned.will print:
A warning about the ordering of items from the documentation: