confused about adding elements to a list with list.append(otherlist)

Tist Verdonck tist.verdonck at mailandnews.com
Mon Aug 13 12:41:24 CEST 2001


I got this problem where I have an empty list and I want to fill it up 
with other lists using a for-iteration:

list = []
data = ['ab','cd','de','ef','gh']
z = len(data)
for x in range(z):
    list.append(data)
    data.append('IJ')

my goal was to get the follwing value for list:
[['ab','cd','de','ef','gh'],
['ab','cd','de','ef','gh','IJ'],
['ab','cd','de','ef','gh','IJ','IJ'],
['ab','cd','de','ef','gh','IJ','IJ','IJ'],
['ab','cd','de','ef','gh','IJ','IJ','IJ','IJ']]

what I got is this:
[['ab','cd','de','ef','gh','IJ','IJ','IJ','IJ','IJ'],
['ab','cd','de','ef','gh','IJ','IJ','IJ','IJ','IJ'],
['ab','cd','de','ef','gh','IJ','IJ','IJ','IJ','IJ'],
['ab','cd','de','ef','gh','IJ','IJ','IJ','IJ','IJ'],
['ab','cd','de','ef','gh','IJ','IJ','IJ','IJ','IJ']]

I think it must have something to do with me having problems with
python's use of variables (not copying them but making references to them)

could somebody please help me out?

Thanks,
Tist Verdonck
tist.verdonck at mailandnews.com



More information about the Python-list mailing list