Appearance
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. Here's an example:
Append an integer to the beginning of a list in Python
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 video course 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.