extension to list extend

Peter Otten __peter__ at web.de
Tue Oct 16 05:47:50 EDT 2007


James Stroud wrote:

> Found that this would be handy today:
> 
>    alist = [1, 2, 3]
>    alist.extend(['a', 'b', 'c'], 1)
>    alist == [1, 'a', 'b', 'c', 2, 3]  # True

A better name for that would be insert(), but that is already used for
single-item insertion. For the time being you can do

>>> items = [1, 2, 3]
>>> items[1:1] = "abc"
>>> items
[1, 'a', 'b', 'c', 2, 3]

Peter



More information about the Python-list mailing list