Skip to content

Instantly share code, notes, and snippets.

@c0ldlimit
Created January 5, 2013 03:11
Show Gist options
  • Select an option

  • Save c0ldlimit/4459497 to your computer and use it in GitHub Desktop.

Select an option

Save c0ldlimit/4459497 to your computer and use it in GitHub Desktop.
#python #dict Using setdefault and from collections import #defaultdict
newdata = {}
for k, v in DATA_SOURCE:
newdata.setdefault(k, []).append(v)
print newdata
from collections import defaultdict
newdata = defaultdict(list)
DATA_SOURCE = (('key1', 'value1'),
('key1', 'value2'),
('key2', 'value3'),
('key2', 'value4'),
('key2', 'value4'),)
for k, v in DATA_SOURCE: newdata[k].append(v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment