Lists into lists

Simon Brunning SBrunning at
Mon Aug 20 17:00:19 CEST 2001

> From:	Jeff Cohen [SMTP:jaif at]
> I want to insert a copy of a list into a list like so:
> >>x=[1,2,3]
> >>y=[4,5,6]
> >> Magic step goes here
> >>x
> [[4,5,6],1,2,3]
> Now, I did this with
> >>x.insert(0,y)
Nearly there. You are inserting y into x, whereas you want to insert a copy.
Two ways to do this. The most common is to take a 'full slice':

x.insert(0, y[:])

The more verbose way is to import the 'copy' module - probably overkill for
this, but worth looking at in case you ever need a deep copy.

Simon Brunning
TriSystems Ltd.
sbrunning at

The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.

More information about the Python-list mailing list