from collections import defaultdict
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(list) # each value is a list
for k, v in s:
d[k].append(v)
print(sorted(d.items())) #[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(dict) # each value is a dict
for k, v in s:
d[k][v] = 0
print(sorted(d.items())) # [('blue', {2: 0, 4: 0}), ('red', {1: 0}), ('yellow', {1: 0, 3: 0})]
k = d.keys()
print(list(k)) # ['yellow', 'blue', 'red']
v = d.values()
print(list(v)) # [{1: 0, 3: 0}, {2: 0, 4: 0}, {1: 0}]
i = d.items()
print(list(i)) # [('yellow', {1: 0, 3: 0}), ('blue', {2: 0, 4: 0}), ('red', {1: 0})]
print(len(d)) # 3