[Tutor] Insert elements in a lis

Kent Johnson kent_johnson at skillsoft.com
Tue Aug 10 19:40:21 CEST 2004


Sure! Use list.insert(index, value):

 >>> l = [1, 2, 3, 5]
 >>> l.insert(3, 4)      # Insert the value 4 at index 3
 >>> l
[1, 2, 3, 4, 5]

Alternately you can use slice assignment to insert a list
 >>> l = [1, 2, 3, 5]
 >>> l[3:3] = [4]
 >>> l
[1, 2, 3, 4, 5]

See http://docs.python.org/lib/typesseq-mutable.html for a list of all the 
ways you can change a list.

Kent

At 06:59 PM 8/10/2004 +0200, Klas Marteleur wrote:
>Hi
>Is it possible to insert elements in a list in an easy way?
>
>example:
>
>Startlist
>[1, 2, 3, 5]
>
>Endlist
>[1, 2, 3, 4, 5 ]
>
>Klas
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list