Python dicts of lists: Grouping values with setdefault

Python dicts of lists: Grouping values with setdefault

Want to create a dict of lists in Python? Use setdefault:

places = {}
while True:
    city, country = input('Place: ').split()
    places.setdefault(country, []) # new country? set value to []
    places[country].append(city)   # list is guaranteed
    print(places)