[Tutor] Prepend to a list?
Liam Clarke
cyresse at gmail.com
Sat Mar 19 22:10:45 CET 2005
Hi Jay,
>>> x = [2,3,4,5,6,7,8]
>>> x.insert(0,1)
>>> print x
[1, 2, 3, 4, 5, 6, 7, 8]
>>> print x.insert.__doc__
L.insert(index, object) -- insert object before index
Or
>>> a = [2,3,4,5,6,7,8]
>>> a.reverse()
>>> a.append(1)
>>> a.reverse()
>>> print a
[1, 2, 3, 4, 5, 6, 7, 8]
>>> print a.reverse.__doc__
L.reverse() -- reverse *IN PLACE*
the IN PLACE is important. It doesn't return a modified copy, it
modifies the original list, same as L.sort()
HTH
Liam Clarke
On Sat, 19 Mar 2005 20:52:16 -0500, Jay Loden <python at jayloden.com> wrote:
> How can I prepend something to a list? I thought that I could do
> list.prepend() since you can do list.append() but apparently not. Any way to
> add something to a list at the beginning, or do I just have to make a new
> list?
>
> -Jay
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.
More information about the Tutor
mailing list