Append data to a list within a dict

7stud bbxx789_05ss at yahoo.com
Sat Apr 14 01:55:19 EDT 2007


On Apr 13, 11:39 pm, Tina I <tina... at bestemselv.com> wrote:
> Hello group,
>
> Say I have the following dictionary:
>
> ListDict = {
> 'one' : ['oneone' , 'onetwo' , 'onethree'],
> 'two' : ['twoone' , 'twotwo', 'twothree'],
> 'three' : ['threeone' , 'threetwo', threethree']}
>
> Now I want to append 'twofour' to the list of the 'two' key but I can't
> figure out how to that?
> Some pointers would be greatly appreciated.
>
> Thanks
> Tina


ListDict["two"] returns the list.  So you can do this:

lst = listDict["two"]
lst.append("twofour")

or you can do it directly like this:

ListDict["two"].append("twofour")





More information about the Python-list mailing list