Dictionaries in Lists

Sean 'Shaleh' Perry shalehperry at attbi.com
Wed Mar 6 16:17:34 EST 2002


On 06-Mar-2002 Avi Homes wrote:
> I was attempting to make a list of dictionaries, which to me feels like the
> C equivlant of an array of structs, kind of. anyway, after making a
> dictionary and putting some values into it, and trying to append it to a
> list, python is not liking this. Am I approaching the idea the wrong way? i
> would like to keep the data organized as dictionaries.. the code looked
> something like
> 
> Info{'IName':"Voodoo2",'IDescription':"blahblah}
> Listing=[]
> Listing.append(Info)
> 

Seems to work here.  What fails for you?

>>> d = {'Foo':1, 'Bar':8}
>>> d
{'Foo': 1, 'Bar': 8}
>>> l = []
>>> l.append(d)
>>> l
[{'Foo': 1, 'Bar': 8}]
>>> l[0]
{'Foo': 1, 'Bar': 8}
>>> l[0]['Foo']
1
>>> y = {'Foo':3, 'Bar':12}
>>> l.append(y)
>>> l
[{'Foo': 1, 'Bar': 8}, {'Foo': 3, 'Bar': 12}]
>>> l[0]
{'Foo': 1, 'Bar': 8}
>>> l[1]
{'Foo': 3, 'Bar': 12}





More information about the Python-list mailing list