Python dict of lists: Using defaultdict from collections

Python dict of lists: Using defaultdict from collections

Want to create a dict of lists in Python? You can use defaultdict:

from collections import defaultdict
places = defaultdict(list)
while True:
    city, country = input('Place: ').split()
    places[country].append(city)   # list is guaranteed
    print(places)