Add integers to a list in Python

avatar

Borislav Hadzhiev

Last updated: Jun 21, 2022

banner

Check out my new book

Add integers to a list in Python #

Use the list.append() method to add integers to a list in Python, e.g. my_list.append(6). The list.append() method adds an item to the end of the list.

main.py
my_list = [2, 4] # ✅ append integer to end of list my_list.append(6) print(my_list) # ✅ append multiple integers to end of list my_list.extend([8, 10]) print(my_list) # ✅ insert integer at specific index of list my_list.insert(0, 100) print(my_list)

The first example uses the list.append method to add an integer to the end of the list.

If you need to add a range of integers to a list, use the range() class.

main.py
my_list = [] for i in range(1, 10): my_list.append(i) print(my_list) # 👉️ [1, 2, 3, 4, 5, 6, 7, 8, 9]

The range class is commonly used for looping a specific number of times in for loops and takes the following parameters:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

If you need to add multiple integers to a list, use the list.extend() method.

main.py
my_list = [2, 4] # ✅ append multiple integers to end of list my_list.extend([8, 10]) print(my_list) # 👉️ [2, 4, 8, 10]

The list.extend method takes an iterable (such as a list) and extends the list by appending all of the items from the iterable.

If you need to add an integer at a specific position in a list, use the insert() method.

main.py
my_list = [2, 4] # ✅ insert integer at specific index of list my_list.insert(0, 100) print(my_list) # 👉️ [100, 2, 4]

Indexes are zero-based in Python. In other words, the first item in the list has an index of 0.

The list.insert method inserts an item at a given position.

The method takes the following 2 parameters:

NameDescription
indexThe index of the element before which to insert
itemThe item to be inserted at the given index
You can also wrap the integers you want to add to the list in square brackets to make them items of another list and use the addition (+) operator to combine the two lists.
main.py
my_list = [2, 4] int_1 = 6 int_2 = 8 new_list = my_list + [int_1, int_2] print(new_list) # 👉️ [2, 4, 6, 8]

When the addition (+) operator is used with 2 lists, we get a new list that contains the items from the two lists.

main.py
print([2, 4] + [6, 8]) # 👉️ [2, 4, 6, 8]

Alternatively, you can use an asterisk * to unpack the list items into a new list that contains the integers you want to add.

main.py
my_list = [2, 4] int_1 = 6 int_2 = 8 new_list = [*my_list, int_1, int_2] print(new_list) # 👉️ [2, 4, 6, 8]

The asterisk * unpacks the items of the list into the new list.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.