Insert at first position of a list in Python [closed] Insert at first position of a list in Python [closed] python python

Insert at first position of a list in Python [closed]


Use insert:

In [1]: ls = [1,2,3]In [2]: ls.insert(0, "new")In [3]: lsOut[3]: ['new', 1, 2, 3]


From the documentation:

list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a),x) is equivalent to a.append(x)

http://docs.python.org/2/tutorial/datastructures.html#more-on-lists