Re: Dictionary with Lists

2009-10-04 Thread Mick Krippendorf
John Nagle schrieb: > Shaun wrote: >> I'm trying to create a dictionary with lists as the value for each >> key. > >Try using a tuple, instead of a list, for each key. Tuples > are immutable, so there's no issue about a key changing while > being used i

Re: Dictionary with Lists

2009-10-04 Thread Shaun
Okay that makes sense. I was assuming that list.append returned the new list. thanks -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionary with Lists

2009-10-03 Thread John Nagle
Shaun wrote: Hi, I'm trying to create a dictionary with lists as the value for each key. I was looking for the most elegant way of doing it... Try using a tuple, instead of a list, for each key. Tuples are immutable, so there's no issue about a key changing while being

Re: Dictionary with Lists

2009-10-03 Thread Mel
Shaun wrote: > testDict = {} > ... > testDict [1] = testDict.get (1, []).append ("Test0") # 1 does not > exist, create empty array > print testDict > testDict [1] = testDict.get (1, []).append ("Test1") > print testDict > [ ... ] > However, the first printout gives {1: None} instead of the desir

Re: Dictionary with Lists

2009-10-03 Thread Mick Krippendorf
Hi, Shaun wrote: > I'm trying to create a dictionary with lists as the value for each > key. I was looking for the most elegant way of doing it... from collections import defaultdict d = defaultdict(list) d["joe"].append("something") d["joe"].app

Dictionary with Lists

2009-10-03 Thread Shaun
Hi, I'm trying to create a dictionary with lists as the value for each key. I was looking for the most elegant way of doing it... I thought this would work: testDict = {} ... testDict [1] = testDict.get (1, []).append ("Test0") # 1 does not exist, create empty array print testD