Adding item in front of a list

Skip Montanaro skip at pobox.com
Wed Apr 9 08:54:22 EDT 2003


    Thomas> I think it would be nice if
    Thomas>  l=[2, 3, 4]
    Thomas>  l.insert(1)

    Thomas> would insert in front of the list, as opposed to append() which
    Thomas> adds new elements at the end.

What about

    >>> lst = [2,3,4]
    >>> lst.insert(0, 1)
    >>> lst
    [1, 2, 3, 4]

Try:

    >>> help([].insert)
    Help on built-in function insert:

    insert(...)
        L.insert(index, object) -- insert object before index

Skip





More information about the Python-list mailing list