Append Integer to List in Python (4 Examples)

 

In this tutorial, you’ll learn how to insert integers to Python lists in the Python programming language.

Table of contents:

Here’s how to do it!

 

Creation of Example Data

Before all else, I’ll create the data we can use in the example syntax later on.

my_list = [1, 2, 3, 'hello']    # Creating a list 
 
print(my_list)                  # Printing my_list 
# [1, 2, 3, 'hello']

As seen, I have created a sample list called my_list containing three integers and one string element. Let’s now jump into the examples!

 

Example 1: Apppend Single Integer to List

In this example, I’ll show how to add a single integer element to a list. For the implementation, I will use the append() method as follows.

my_list.append(4)               # Appending an integer value
 
print(my_list)                  # Printing my_list 
# [1, 2, 3, 'hello', 4]

You can see how the integer 4 has been appended to the end of my_list. Let’s see how we can add multiple integers to a list next!

 

Example 2: Append Multiple Integers to List using extend()

When the goal is to insert multiple integers into a list, the extend() method is commonly employed. For demonstration, two integers, 4 and 5, will be added to my_list below.

my_list.extend((4, 5))          # Appending multiple integer values
 
print(my_list)                  # Printing my_list
# [1, 2, 3, 'hello', 4, 5]

Looks great! Both 4 and 5 are now appended to my_list. If you wonder if using the append() method can also add multiple integers to lists, the answer is yes. See the next example.

 

Example 3: Append Multiple Integers to List using append()

In order to use the append() method for adding multiple integer numbers to a list, we need to set a for loop to repeat the appending operation for each integer item.

for item in [4, 5]:             # Appending multiple integer values via for loop
    my_list.append(item)
 
print(my_list)                  # Printing my_list
# [1, 2, 3, 'hello', 4, 5]

As shown, 4 and 5 have been appended to my_list in the first and second iterations of the for loop. Last but not least, using the concatenation operator also enables to add multiple integers to a list. However, it is a less common method, possibly due to its working out-of-place principle, which will be discussed in the next example.

 

Example 4: Append Multiple Integers to List using Concatenation Symbol

This example explains how to use the + operator to insert integer numbers into a list. All needs to be done is to create a list containing integer elements to be included, then add it to the existing list via the + symbol.

my_list + [4, 5]                # Appending multiple integer values by concatenation
# [1, 2, 3, 'hello', 4, 5]

The concatenation works differently from the append() and extend() methods. The latter ones work in place, which means they modify the object directly. On the other hand, the original object remains the same when the concatenation operator is used.

print(my_list)                  # Printing my_list
# [1, 2, 3, 'hello']

As seen, my_list still does not have the integer elements. The operation should have been assigned to the object explicitly.

 

Video, Further Resources & Summary

Do you need more info on the content of this post? Then you may have a look at the following video on my YouTube channel. I’m explaining the content of this tutorial in the video tutorial.

 

The YouTube video will be added soon.

 

Also, you could have a look at the other articles on my website.

 

At this point, you should have learned how to include integers in a list in Python. Don’t hesitate to let me know in the comments section below if you have further comments and/or questions.

 

Cansu Kebabci R Programmer & Data Scientist

This page was created in collaboration with Cansu Kebabci. Look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview of her other tasks on Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top