complex data structure - insert value

Jeffrey Froman jeffrey at fro.man
Sun Oct 10 01:01:23 CEST 2004


spar wrote:

> Basically I'd like to do the same thing in Python. I've written a
> convoluted test script where I can print an element:
> 
> print data[0]['CC'][1]
> 
> but I can't insert directly like I do in Perl (data[0]['CC'][1] =
> $var).

This should work fine, though in python this would result in assignment, not
insertion:

***************************
>>> #Assignment:
>>> x=[{'foo':[1,2,3]}]
>>> x[0]['foo'][1] = 'bar'
>>> x
[{'foo': [1, 'bar', 3]}]

>>> #Insertion:
>>> x[0]['foo'].insert(2, 'baz')
>>> x
[{'foo': [1, 'bar', 'baz', 3]}]

***************************

Jeffrey



More information about the Python-list mailing list