Home » Python » Python List append Method

About Yatin

Working as a Technical Lead in the Information Technology sector where I'm primarily involved with projects based on Java/J2EE Technologies platform (such as Spring, Spring Boot, Hibernate, Redis, etc.), novel UI technologies (Angular & React), Docker, and AWS. Recently completed the AWS Solution Architect Associate Certification.

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 syntax as –

Syntax

list.append(new_element)

The method accepts a single argument which is added to the original list. This argument can be number, 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 learned –

  • Introduction to python list append method in the programming
  • Sample programming stuff

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.

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

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

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments