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

Question

Asked By – Fr0z3n7

How can I insert an element at the first index of a list?
If I use list.insert(0, elem), does elem modify the content of the first index?
Or do I have to create a new list with the first elem and then copy the old list inside this new one?

Now we will see solution for issue: Insert at first position of a list in Python [closed]


Answer

Use insert:

In [1]: ls = [1,2,3]

In [2]: ls.insert(0, "new")

In [3]: ls
Out[3]: ['new', 1, 2, 3]

This question is answered By – michel-slm

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0