In this Python post, we’re going to make use of a practical Python example code in order to practically demonstrate the way to utilize Python list append.
After reading this article, you will have a practical understanding of how to use Python list append. So let our discussion drop a while and dive right into its implementation.
What is Python List Append?
It is used to add items to the Python list. It add items to the last of list, means after the last item of Python list. It takes an object and add it to the last of list.
Let’s now go through multiple examples in which Python list append will be practically implemented.
Implementing Python List Append (Multiple Examples)
For that, we have to use the append method of Python list. See below syntax:
Let’s now see practical examples to implement this.
Example: Add Single Item To List
pythonLst=[1,2,3,4] print(pythonLst) pythonLst.append(6) //append it like this print(pythonLst)
[1, 2, 3, 4] [1, 2, 3, 4, 6]
You can see that we have appended an integer to the list. We can use a string or double as well and it will be appended as well. See below code:
pythonLst=[1,2,3,4] print(pythonLst) pythonLst.append('45.3') print(pythonLst) pythonLst.append('gk') print(pythonLst)
[1, 2, 3, 4] [1, 2, 3, 4, '45.3'] [1, 2, 3, 4, '45.3', 'gk']
Append Adds Only Single Item
Append method adds only single item to the list. It means you can add integer, string, double, list, tuple, dictionary or custom object.
When you try to pass list, tuple or dictionary to the append to be added to the list, then it will be added as a single item. See below examples.
Example 1: Appending List To List
pythonLst=[1,2,3,4] pythonLst.append([87,45,'rt']) print(pythonLst)
[1, 2, 3, 4, [87, 45, 'rt']]
You can see that the list is added as a single item.
Example 2: Appending Tuple To List
pythonLst=[5,6,7,8] pythonLst.append((74,23,'tr')) print(pythonLst)
[5, 6, 7, 8, (74, 23, 'tr')]
The tuple is also added as a single item to the existing list.
Use Extend To Append Each Item Of List Or Tuple
If you want to add each item of the list of tuple to add to the existing list, then you have to use the extend method of Python list. See below:
pythonLst=[4,2,7,9] pythonLst.extend((23,98,'tr')) print(pythonLst)
[4, 2, 7, 9, 23, 98, 'tr']
Extend method unpacks each item whether its list of tuple, and adds it to the end of the existing list as shown in the above image. You can use the same method to append each item of new list to existing list.
Append Returns None
Append doesn’t return a new list. It actually just add the items to the existing list. Append returns none. See below code:
pythonLst=[4,2,7,9] newList=pythonLst.append('green') print(newList) print(pythonLst)
None [4, 2, 7, 9, 'green']
As you can see the newList returns none and the existing list shows an added item to it.
So this is how you can easily use Python list append. If you still have any doubts related to the customization of Python list append, then do let me know in the comment section. I’d be very glad to answer all.
As conclusion, hope you now have a clear idea of how to properly use Python list append. We have used multiple examples to demonstrate its usage. Your feedback would be well appreciated. Thank you for reading this post.