0

Hello :) I wrote a little code that creates a dictionary of three people and their favorite numbers.

favorite_numbers={"lore":[5, 19], "louise":[7,24],"annie":[1,2]}

I'd want an output that prints the name of each person along their favorite numbers

for prs, numb in favorite_numbers.items():
    pers= prs
    numb= favorite_numbers[prs]
    print(f"{pers}'s fav numbers are {numb}")

this is how i've done it. the thing I dislike, however, is that the numbers appear in square brackets my output ís:

lore's fav numbers are [5, 19]
louise's fav numbers are [7, 24]
annie's fav numbers are [1, 2]

I'd want it to be:

lore's fav numbers are 5, 19
louise's fav numbers are 7, 24
annie's fav numbers are 1, 2

so my question is, how should I print my dictionary so that I get rid of the brackets? Thank you in advance

  • When using `f""` to print something, whatever you place inside the curly brackets is interpreted. Taking advantage of that and of the fact that `[` and `]` will always be the first and last characters, you can write `{str(numb)[1:-1]}` to have the first and last pieces of the dictionary's string representation removed. – berna1111 Jan 03 '22 at 23:33
  • `numb` is a list, and this is the default way how python prints out lists. – Gino Mempin Jan 03 '22 at 23:41
  • Does this answer your question? [How to print a list of numbers without square brackets?](https://stackoverflow.com/q/53111469/2745495) – Gino Mempin Jan 03 '22 at 23:45

1 Answers1

1

You can use

', '.join(map(str, numb)) 

inside the curly brackets in place of numb. This line converts numbers to strings and joins the strings with a comma and space between them.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
MYousefi
  • 978
  • 1
  • 5
  • 10