How to Append Integer to Beginning of List in Python
Created
Modified
Using insert Method
The list.insert()
method inserts an item at a given position. The first argument is the index of the element before which to insert. For example,
#!/usr/bin/python3
a = [1, 2, 3]
a.insert(0, 5)
print(a)
[5, 1, 2, 3]
Using Unpack List
The following example should cover whatever you are trying to do:
#!/usr/bin/python3
a = [1, 2, 3]
a = [5, *a]
print(a)
[5, 1, 2, 3]