Hi Python Lovers, In this Today article we are about to learn everything about the Python list append() method with the help of the examples. To work with the append() method, you must know about the Python list because the append() method will always apply on top of the Python List.
As we know Python is popular for its simplicity and elegance. There are various data structures available in Python but one of the frequently used data structures in Python is list. A list in Python is an Ordered, Indexed, and Mutable data structure where mutable means we can change the items of the list once it has been created.
Now, Let’s see all about the append() method.
Headings of Contents
Python List append() Method:
In Python List, The append() is one of the widely used methods to add new items to the list. The append() method always adds a new item at the end of the existing list. The append() method directly modifies the original list in place which means it directly affects the list to which it is applied.
Syntax of append():
The syntax of append() in Python is.
list.append(item)
Python List append() Method Parameter:
The list append() method takes only a single parameter called an item that will be added to the list.
- item:- An item to be added to the end of the list.
The item can be numbers, strings, dictionaries, another list, etc.
Return Value of list append() method:
list append method in Python does not return any value ( Return None ).
How append() Works
The append() method adds its argument to the end of the list and it does not return any value instead it modifies the existing list in place. Each time length of the list will be increased by 1 after calling the append() method.
Examples of append() Method
Various use cases of the append() method like append string to list, append integer value to list append dictionary to list, etc.
Appending string to the list:
I have created a simple Python list called fruits. Now, I am about to add an element to the fruits
list:
fruits = ['apple', 'banana', 'cherry']
fruits.append("orange")
print(fruits)
Output
['apple', 'banana', 'cherry', 'orange']
Now you can see the orange fruit has been added to the end of the list of fruits.
Appending dictionary to the list:
I have created a Python dictionary called a car and Now I’m about to add a car dictionary to the end of the list using the append() method.
fruits = ['apple', 'banana', 'cherry']
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
fruits.append(car)
print(fruits)
Output
['apple', 'banana', 'cherry', {'brand': 'Ford', 'model': 'Mustang', 'year': 1964}]
As you can see car dictionary has been added to the list.
Appending integer to the list
You can also an integer value to the end of the list by using the append() method, Let’s see.
fruits = ['apple', 'banana', 'cherry']
value = 100
fruits.append(value)
print(fruits)
Output
['apple', 'banana', 'cherry', 100]
Appending list to the list
Even we can append a list to the end of the existing list.
fruits = ['apple', 'banana', 'cherry']
new_list = ['orange', 'banana']
fruits.append(new_list)
print(fruits)
Output
['apple', 'banana', 'cherry', ['orange', 'banana']]
Here, you need to remember one thing, When you add a new list to the end of the existing list, The entire list will be added to the end of the list as a single list, not its elements.
Using append() in Loops
The list append() can be also used inside Python for loop. Let’s see how can use the append() method to add an item to the end of the list.
squares = []
for i in range(1, 6):
squares.append(i ** 2)
print(squares)
Output
[1, 4, 9, 16, 25]
Here, we append the square of each number from 1 to 5 to the squares
list.
Recommended Tutorials:
- List append() method
- List clear() method
- List count() method
- List copy() method
- List extend() method
- List index() method
- List insert() method
- List reverse() method
- List pop() method
Conclusion
Python list append() method is a powerful method to dynamically modify the Python list. It is simple, intuitive, and efficient for adding elements to the end of a list. Whenever you are working with a single Python list then the append() method will be very useful if you want to add a new item to the end of the existing list.
If you found this Python list append() method article helpful, Please share and keep visiting for Python tutorials.
If you like this post please comment and share it with your friends who want to learn Python programming.
Python List Reference, Click here