[Tutor] Strange Appending

Marilyn Davis marilyn at deliberate.com
Thu Dec 2 23:41:03 CET 2004


On Thu, 2 Dec 2004, mdcooper wrote:

> Hello,
> 
> I am trying to append a list to another list, but everytime I do, the new 
> parent list has a new child list, but all the other lists have become the same 
> as the new child list.
> 
> Code:
> 
> 
> self._f.write(str(self.residue.atoms[int(t[0])-1].element) + ' ')
>             for m in t:
>                 self._f.write(str(m)+' ')
>             self._f.write('\n')
> 
>             self.a.append(t) # WHY DOES THIS NOT WORK?????

Hi, 

I'm not sure that I understand your question because I don't see all
the code and I don't know what you hope will happen.  But ...

append appends the object as a single element.

Try self.a.extend(t)

extend attaches the t list to the end of the list.

Does this give you what you expect?

Marilyn Davis

>             print self.a
> 
> Output:
> 
> [[1, 234, 543]]
> [[1, 234, 548], [1, 234, 548]]
> [[1, 234, 59], [1, 234, 59], [1, 234, 59]]
> [[1, 237, 543], [1, 237, 543], [1, 237, 543], [1, 237, 543]]
> 
> 
> Can anyone help?
> 
> thanks,
> 
> Matthew (mdcooper at uvic dot ca)
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 



More information about the Tutor mailing list