Quicker solution to the problem with loops and dictionaries

What you are missing is a join and items functions. join joins all items of iterable with specified delimiter. If there is only 1 item, no delimiter will be added. It is generally considered a bad practise to concatenate strings with + or +=. Strings are immutable and thus each concatenation produces a new string, that has to have memory allocated for it. To avoid this, you could first collect all parts of strings into list (which you already have as an input) and then join them.

items() returns tuples of (key, value) of dictionary.

favourite_places = {
    "Jozefína": ["Amsterdam", "Liverpool", "London"],
    "Marek": ["Košice"],
    "Jozef": ["Lisabon", "Vancouver"]

for name, places in favourite_places.items():
    print(f"{name} likes to go to here: {', '.join(places)}.")

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top