Taking values from a for

Asked

Viewed 39 times

-2

Very simple doubt indeed. I have the following list:

fruits = {"apple":"123", "banana":"456", "cherry":"789"}
for fruit in fruits:
  print(fruit) 

How do I stop in my return appear the numbers of each?

The way the return is: apple, banana and Cherry

but I need the return to be 123,456,789

2 answers

1


You can use the value you have already extracted as a key in the dictionary:

fruits = {"apple":"123", "banana":"456", "cherry":"789"}
for fruit in fruits:
  print(fruits[fruit]) 

or ask the values of the dictionary with values():

fruits = {"apple":"123", "banana":"456", "cherry":"789"}
for value in fruits.values():
  print(value)
  • Just like that! Top! Thank you

-1

You should print like this:

print(fruits[fruit])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.