0

I declare a dictionary named Exemple-diccionari with different names and phone numbers. Then the user enters a new name and a new number that will be added as a new item to the dictionary. I want the console to print the dictionary, but with this format: Name + Phone nº Name2 + Phone º2 ...

Instead of {'Name': 'Phone nº', 'Name2': 'Phone nº2', ...}

I was thinking about using a for statement what do you think? Here's the code:

Dictionary={'Name':'Phone n','Name2':'Phone n2','Name3':'Phone n3'}

First=input('Whats the name? ')
Second=input('And the phone number? ')
Dictionary[str(Primer)]=str(Segundo)

counter=1
for counter in range(1, len(Exemple_diccionari)):
    print(Exemple_diccionari[contador])

I don't know exactly how to continue neither if the for is right. Thank you

1 Answers1

2

You will get more details here but to make it short, you can try the following with list comprehension :

[print(value) for key, value in dictionary.items()]

or with a classical for loop :

for value in dictionary.values():
    print(value)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
R Liab
  • 156
  • 2
  • Your list comprehension is creating a list just for its side effect (and you are not even using the returned keys -- why didn't you use dictionary.values() instead?). I am not a big fan of this. See: [Is it Pythonic to use list comprehensions for just side effects?](https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects). – Booboo Apr 29 '20 at 13:51
  • You're right for the list creation, I use it only for short examples where the list size is not really important. Event if it is not very Pythonic, I like the short notation. For the items() call instead of values() it was only to show that he can also this function if he wants to print the keys as well :) – R Liab Apr 30 '20 at 08:37