Python List append()

In this article, we will learn about the append() method of List in Python.

Table Of Contents

Introduction

List provides a function append(elem) to an element at the end of the calling List object. It accepts an element as an argument, and appends that element at the end of List.

Syntax of append() method of List

list.append(element)

Parameters of append() method

It accepts only one argument,

  • element: An object that needs to be added in the list. It can be an integer, string, boolean, tuple, list or any other object.

Return value of append() method

It adds the given element inside the list in place. Therefore it returns None, basically it returns nothing.

Advertisements

Examples of List append()

Add a string to the end of a List of strings

Suppose we have a list of five strings, and we want to append a new string into this list. For that we will call the append() function of list, and pass new string element into it. It will add this new string at the end of list. Let’s see the complete example,

listOfNames = ['John', 'Mark', 'Ritika', 'Atharv']

# Add a string at the end of list
listOfNames.append('Smriti')

print(listOfNames)

Output:

['John', 'Mark', 'Ritika', 'Atharv', 'Smriti']

Add an integer to the end of a List

Suppose we have a list of five numbers, and we want to append a new number into this list. For that we will call the append() function of list, and pass new number into it. It will add this new number at the end of list. Let’s see the complete example,

listOfNumbers = [11, 12, 13, 14, 15]

# Add a number at the end of list
listOfNumbers.append(99)

print(listOfNumbers)

Output:

[11, 12, 13, 14, 15, 99]

Add a tuple the end of a List

Suppose we have a list of five tuples, and we want to append a new tuple into this list. For that we will call the append() function of list, and pass new tuple into it. It will add this new tuple at the end of list. Let’s see the complete example,

listOfStudents = [ ('Shaun, 31'), 
                   ('Mark, 32'),
                   ('Ritika, 33'),
                   ('Lisa, 34')]


# Add a tuple at the end of list
listOfStudents.append(('Atharv', 28))

print(listOfStudents)

Output:

['Shaun, 31', 'Mark, 32', 'Ritika, 33', 'Lisa, 34', ('Atharv', 28)]

Summary

We learned all about the append() method of list in Python. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top