Created
January 5, 2013 03:11
-
-
Save c0ldlimit/4459497 to your computer and use it in GitHub Desktop.
#python #dict Using setdefault and from collections import #defaultdict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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