Python List append Method
Hello in this tutorial, we will see how to use to list append() method in python programming.
1. Introduction
The append()
method adds a new element to the end of the list and it returns nothing. It is represented by the following syntax:
Syntax
list.append(new_element)
The method accepts a single argument that is added to the original list. This argument can be numbers, strings, dictionaries, lists, etc.
1.1 Setting up Python
If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.
2. Python List append Method
Let us dive in with the programming stuff now. I am using JetBrains PyCharm as my preferred IDE. You’re free to choose the IDE of your choice.
2.1 Creating an implementation file
Let us understand the implementation with the help of a python script.
Implementation file
# python list append method # append(..) method adds an item to the end of the list and it returns nothing def append_ele_to_list(items, element): print('adding single element') items.append(element) print('updated list = {}\n'.format(items)) def append_list_to_list(items, new_items): print('adding list') items.append(new_items) print('updated list = {}\n'.format(items)) # driver code if __name__ == '__main__': phones = ['Apple', 'Android', 'Blackberry', 'Windows', 'Galaxy', 'HTC'] print('original list = {}\n'.format(phones)) # append_ele_to_list(phones, 'Samsung') # append_list_to_list(phones, ['Motorola', 'Lenovo'])
You can uncomment the method of your choice and run this python script. If everything goes well the output will be shown in the IDE console.
Console output
original list = ['Apple', 'Android', 'Blackberry', 'Windows', 'Galaxy', 'HTC'] adding single element updated list = ['Apple', 'Android', 'Blackberry', 'Windows', 'Galaxy', 'HTC', 'Samsung'] adding list updated list = ['Apple', 'Android', 'Blackberry', 'Windows', 'Galaxy', 'HTC', 'Samsung', ['Motorola', 'Lenovo']]
That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!
3. Summary
In this tutorial:
- We were introduced to the Python list
append
method in the programming - We practiced through examples.
You can download the source code of this tutorial from the Downloads section.
4. More articles
5. Download the Project
This was a tutorial to understand the while loop in python programming.
You can download the full source code of this example here: Python List append Method
Last updated on Jan. 4th, 2021