Python list append() method adds an element to the end of the list.
Python List append() Syntax
The append() method adds a single item to the existing list. The original list length is increased by 1. It is one of the most popular list methods.
The append() method syntax is:
1 |
list.append(element) |
The append() method takes a single parameter, which is appended to the end of the list. Python list is mutable.
The element can be a number, string, object, list, etc. We can store different types of elements in a list.
List append() return value
The list append() method doesn’t return anything. You can also say that the append() method returns None
.
Python List append() Example
Let’s look at a simple example to add an item to the end of the list.
1 2 3 4 5 6 7 |
vowels = ['a', 'e', 'i'] print(f'Original List is {vowels}') vowels.append('o') vowels.append('u') print(f'Modified List is {vowels}') |
Output:
1 2 3 |
<strong><span style="color: #008000;">Original List is ['a', 'e', 'i'] Modified List is ['a', 'e', 'i', 'o', 'u'] </span></strong> |
Appending List to another List
If we pass a list to the append() method, it gets added as a single item to the end of the list.
1 2 3 4 5 6 7 |
list_numbers = [1, 2, 3] list_primes = [2, 3, 5, 7] list_numbers.append(list_primes) print(f'List after appending another list {list_numbers}') |
Output:
1 2 |
<span style="color: #008000;"><strong>List after appending another list [1, 2, 3, [2, 3, 5, 7]] </strong></span> |
Tip: If you want to append the elements of a list to another list, use the list extend() method.
1 2 3 4 5 6 7 |
list_numbers_odd = [1, 3, 5] list_numbers_even = [2, 4, 6, 8] list_numbers_odd.extend(list_numbers_even) print(f'List after extending from another list {list_numbers_odd}') |
Output:
1 2 |
<span style="color: #008000;"><strong>List after extending from another list [1, 3, 5, 2, 4, 6, 8] </strong></span> |
Conclusion
Python List append() method allows us to add any type of data to the end of the list. The method doesn’t return anything. The original list is modified and the size is increased by 1.