SH Johnson
0
Q:

multiple values for one key python

 from collections import defaultdict
 data = [(2010, 2), (2009, 4), (1989, 8), (2009, 7)]
 d = defaultdict(list)
print (d) # output --> defaultdict(<type 'list'>, {})
 for year, month in data:
     d[year].append(month) 
print (d) # output --> defaultdict(<type 'list'>, {2009: [4, 7], 2010: [2], 1989: [8]})
2
example = {}
example.setdefault('a', []).append('apple')
example.setdefault('b', []).append('boots')
example.setdefault('c', []).append('cat')
example.setdefault('a', []).append('ant')
example.setdefault('a', []).append('apple')
1

New to Communities?

Join the community