confused about adding elements to a list with list.append(otherlist)
Emile van Sebille
emile at fenx.com
Mon Aug 13 13:31:36 CEST 2001
Two things: You probably want list.append(data[:]) - this makes a copy of
data when appending to list; and you probably shouldn't use list as an
identifier as it's also a keyword that converts to a list (try:
list("123")).
HTH,
--
Emile van Sebille
emile at fenx.com
---------
"Tist Verdonck" <tist.verdonck at mailandnews.com> wrote in message
news:8e84aae6.0108130241.2163205c at posting.google.com...
> 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