Append to the Beginning of a List in Python

  • Post category:List

We can append elements to the beginning of a list in Python using the insert() method with an argument of 0 to insert the element at the beginning, or by using the + operator to concatenate a new list with the original list. Here are two examples:

Example 1:

numbers = [2, 3, 4, 5]
numbers.insert(0, 1)
print(numbers)

Output:

[1, 2, 3, 4, 5]

Example 2:

numbers = [2, 3, 4, 5]
numbers = [1] + numbers
print(numbers)

Output:

[1, 2, 3, 4, 5]