collections.defaultdict
Completed
Read n lines city person. Using defaultdict(list), print each city (sorted) followed by its people (sorted).
Input
Line 1: n. Then n lines.
Output
One line per city.
Example
Input
4 pune zed delhi amy pune bob goa cy
Output
delhi amy goa cy pune bob zed
Editor
Python
Hints
defaultdict(list) creates an empty list the first time a key is used.
Solution explanation
defaultdict removes the "is this key here yet?" check.
Show solution
from collections import defaultdict
by_city = defaultdict(list)
for _ in range(int(input())):
city, person = input().split()
by_city[city].append(person)
for city in sorted(by_city):
print(city, *sorted(by_city[city]))