In this Python tutorial, we will discuss the Python List append() method which is used to add items to a list. Let’s take a few examples and see how we can use the list append() method in Python.
List append() Method in Python
Below are the topics that we are doing to discuss in this article:
- Introduction to the Python List append() method
- Syntax of the append() method in Python Lists
- How to use the append() method to add elements to a list
- Examples of using the append() method in Python Lists
- Why the append() method is a useful tool for working with Python Lists
Python List append() method
The append() method is a built-in function in Python that allows us to add an item to the end of an existing list. This method modifies the original list and returns None.
The syntax for the append() method is as follows:
list.append(item)
Here, “list” is the name of the list to which the item is to be added, and “item” is the element that is to be added.
The append() method takes a single argument, which can be any data type of Python, such as a string, integer, or another list. When the append() method is executed, it adds the item to the end of the list and increases the size of the list by one.
append() method to add elements to Python list
Let’s take a look at some examples to see how the append() method works in Python:
Example 1:
countries = ['Brazil', 'United States', 'Mexico']
countries.append('Canada')
print(countries)
In the first example, we have a list of countries, and we use the append() method to add the string ‘Canada’ to the end of the Python list.
Output:
Example 2:
numbers = [1, 2, 3]
numbers.append(4)
print(numbers)
In the second example, we have a list of numbers, and we use the Python append() method to add integer 4 to the end of the list.
Output:
Example 3:
my_list = []
my_list.append('USA')
my_list.append(123)
my_list.append(3.14)
print(my_list)
In the third example, we have an empty list, and we use the append() method to add a string, an integer, and a float to a Python list.
Output:
It’s important to note that the append() method modifies the original list in Python and does not create a new list. If we try to assign the result of the Python append() method to a new variable, we will get a NoneType object, which represents the absence of a value.
Let’s take a look at an example:
currency = ['US Dollar', 'British Pound', 'Swiss Franc',]
new_list = currency.append('Euro')
print(new_list)
In this example, we try to assign the result of the Python List append() method to a new variable, but when we print the new variable, we get None instead of the updated list.
Output:
Note: To avoid this, we should use the append() method directly on the original list.
append() method in Python Lists Examples
The append() method is a simple and efficient way to add elements to a Python list. It is commonly used in many applications where we need to add new elements to a list dynamically.
Additionally, since the append() method modifies the original list, we can use it in a loop to dynamically build a list of elements. Here are some common use cases for the Python append() method with examples:
Building a Python list dynamically
When we want to create a list dynamically by adding new elements to it one at a time in Python, we can use the append() method.
For example, if we are parsing a file and want to store the lines in a list, we can use the append() method to add each line to the list as we read it.
lines = []
with open('file.txt') as f:
for line in f:
lines.append(line.strip())
print(lines)
In this example, we are reading the lines from a file and storing them in a list using the Python List append() method. Each line is stripped of any leading or trailing whitespace before it is added to the list.
Output:
Appending to a list inside a loop
When we are iterating over a loop and want to add new elements to a Python list based on the loop variable, we can use the append() method inside a for loop.
For example, if we are generating a list of even numbers, we can use the append() method to add each even number to the list inside the loop.
even_numbers = []
for i in range(10):
if i % 2 == 0:
even_numbers.append(i)
print(even_numbers)
In this example, we are generating a Python list of even numbers by iterating over for loop and using the append() method to add each even number to the list.
Output:
Add elements to a Python list after it has been created
When we have an existing list and want to add new elements to it, we can use the append() method to add the new elements to the end of the Python list.
countries = ['Alabama', 'Alaska', 'Arkansas']
countries.append('California')
print(countries)
In this example, we have an existing list of states, and we use the append() method to add the string ‘California’ to the end of the Python list.
Output:
Concatenate lists
We can use the Python append() method to concatenate two lists by adding all the elements of one list to the end of the other list. This can be done by using a loop to iterate over one list and append each element to the other list.
list1 = [1, 2, 3]
list2 = [4, 5, 6]
for item in list2:
list1.append(item)
print(list1)
In this example, we are concatenating two Python lists by iterating over one list (list2) and using the append() method to add each element to the other list (list1).
Output:
Conclusion
The append() method is a useful function in Python that allows us to add items to the end of a list. It’s a simple and efficient way to modify a list and can be used in a variety of scenarios.
Remember that the Python append() method modifies the original list and does not create a new list, so it’s important to use it correctly to avoid unexpected behavior in your code.
You may like the following Python lists tutorials:
- How to Create a String from a List in Python
- Convert NumPy Array to List of Lists in Python
- How to Convert NumPy Tuple to List in Python
- convert numpy array to list of strings in Python
- Add Numbers in a List in Python Without Sum
- Python List extend() method [With Examples]
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.