W3docs

Append integer to beginning of list in Python

To append an integer to the beginning of a list in Python, you can use the insert() method.

To append an integer to the beginning of a list in Python, you can use the insert() method. Here's an example:

Append an integer to the beginning of a list in Python

my_list = [1, 2, 3]
my_list.insert(0, 4)
print(my_list) # Output: [4, 1, 2, 3]

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

In the example above, the insert() method is used to insert the integer 4 at the index 0 of the list my_list. This effectively adds the integer to the beginning of the list. The first argument to the insert() method is the index where the element should be inserted, and the second argument is the element itself.