This is a tutorial of the Python List append() Method. Learn to add any new item at the end of a Python List using the append() method with examples.
Python List append() Method
append() method is used to append or add a new item (which can be any Python object) at the end of an existing Python List.
append() only takes a single argument that is the item which is needed to be added or appended at the end of the list. As mentioned earlier, this item can be any Python Object such as an Integer, String, another List, Dictionary or anything else.
append() Return Value
Obviously, this method would not have anything to return, so it simply returns
This method is quite easy and quick to use. Have a look at the following examples to quickly understand the different use cases of the Python List
Example 1. Appending a new Element into List
#Python List append() Method Example: 1
#List of Fruits
fruits = ["Apple", "Banana", "Mango"]
#Add a new fruit item into the fruits List
#The new item has now been added
#at the end of the list
In the above python code, we initially have a List defined for different fruit names. Then we’ve used the append method to add a new fruit into the list. Then we’re simply using the
print() statement to check the updated list.
Example 2. Appending another List into an existing List
#Python List append() Method Example: 2
#List of Outdoor Games
outdoor_games = ["Cricket","Football","Basketball","Hockey"]
#List of Indoor Games
indoor_games = ["Chess","Ludo","Snakes & Ladders"]
#appending indoor_games List to outdoor_games List
#Checking updated List
Here, we’re just appending a List named
indoor_games into another list named
Note. Observe that the new list is appended into the existing list as a single item. Therefore, the items of the list
indoor_games are actually the sub-items of the single-appended list. This is also an example of a Nested List created using the Python List
append() method. If you just want to merge or concatenate two lists, you can use the
+ operator between two lists that you want to merge. I have already illustrated this in the following linked article.
I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.
And do not forget to subscribe to WTMatter!