Python

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

1
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.

python list append

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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

1
2
3
4
5
6
7
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.

5. Download the Project

This was a tutorial to understand the while loop in python programming.

Download
You can download the full source code of this example here: Python List append Method

Last updated on Jan. 4th, 2021

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button