The following two expressions seem equivalent to me. Which one is preferable?
data = [(\'a\', 1), (\'b\', 1), (\'b\', 2)] d1 = {} d2 = {} for key, val in d
You might want to look at defaultdict in the collections module. The following is equivalent to your examples.
defaultdict
collections
from collections import defaultdict data = [('a', 1), ('b', 1), ('b', 2)] d = defaultdict(list) for k, v in data: d[k].append(v)
There's more here.