List append() Method in Python Explained with Examples

Chirag Goyal Last Updated : 29 Oct, 2024
8 min read

The append() method in Python resides within the list class, offering ingenious functionality that streamlines the addition of items to lists and introduces flexibility, significantly enhancing program efficiency. Join us as we delve into the nuances of the append function in Python, exploring its diverse applications. Whether you’re a novice or a seasoned developer, this article guarantees valuable insights into maximizing the potential of append() in Python.

Learning Objectives:

  • Understand the purpose and functionality of the append() method in Python for working with lists.
  • Learn the syntax and usage of the append() method, including the parameters it accepts.
  • Explore various examples demonstrating how to append different data types (integers, strings, booleans, tuples, dictionaries, etc.) to a list using append().
  • Recognize the advantages and use cases of the append() method, such as dynamically building lists and modifying existing lists efficiently.

So let’s get started and take a closer look at the append() method in Python!

List Collection Type

In Python, lists are versatile and widely used data structures capable of storing heterogeneous elements in a single variable. Unlike arrays, which store elements of the same data type, lists provide flexibility, making them essential in programming.

Properties of List Collection Type in Python

This data type possesses the following key properties:

  • Lists maintain order, are changeable, and permit duplicate values.
  • They exhibit remarkable versatility, finding application in various programming scenarios.

Now, let’s delve into the append() method, a crucial operation within this extensive collection, and discuss its role.

To understand the List methods, refer to one of the beginner-level articles from Analytics Vidhya.

Role of the Python List Append Method

  • It is a built-in function that is used to add an item to the end of a list.
  • It is a common and powerful method for modifying lists in terms of elements and updating the same variable itself.
  • It allows you to easily add new elements to a list without creating a new list each time.

Advantage of the Append Method

One of the main advantages of this method, evident from its implementation, is its ability to dynamically build lists rather than statically. This flexibility is particularly useful when the number of items to add is unknown beforehand.

Overall, the append function python is an essential tool for working with lists in Python, and mastering it will help you become a more efficient and effective programmer.

Append Method in Python

In Python, the append() function is a built-in function used to add an item to the end of a list.

The append() method is a member of the list object, and it is used to modify the contents of an existing list by adding a new element to the end of the list. The append function returns nothing.

How append works?

Using this method involves a straightforward function call to an existing list object, passing the new item as an argument to add it to the end of the list.

Every time we call this method on any existing list, this method adds a new item to the end of the list. You can refer to the link to learn more about the append function.

Syntax of the append() Method

The append() method appends an element to the end of the list.

Syntax:

list.append(element_to_be_added)

The parameter used in this method is shown below:

Parameter: element_to_be_added

Description: This parameter is required for this method which may be an element of any data type (string, integer, float, object, etc.).

Therefore, we can see that this method takes a single argument, basically the item we must add to the end of the list. Calling this method on a list adds the specified item to the end, consequently increasing the list’s length by one.

Top 10 Examples of Append Method in Python

Now, let’s see how we can use this append function python as iterables or iterators to do the following functionalities. In all the following examples, we utilize Python 3, a language widely employed in technologies such as:

  • Data Science,
  • Machine Learning,
  • Deep learning, etc.

Other programming languages like Java, JavaScript, etc., can implement similar functionalities to these methods. Some Python libraries include:

  • Numpy for Matrix Manipulations,
  • Pandas for Data Analysis using Pandas DataFrame,
  • Matplotlib for Data Visualisation,
  • Tensorflow for Computer Vision,
  • NLTK for Natural Language Processing, etc.

Example 1: Pushing a Single Item to a List

In this example, we see a list of fruits where we add a new fruit, increasing the list’s length by one. It’s important to note that this method does not return any value.

# Created a list 

fruits = ["grapes", "banana", "cherry", "orange"]

# Add a new element to the list of the above fruits

fruits.append("apple")

# Print the updated list

print(fruits)

The output of the above program is shown below:

["grapes", "banana", "cherry", "orange", "apple"]

Example 2: Pushing Multiple Items to a List Using a For Loop

In this example, you can see that we have a list of fruits, and we are trying to add some new fruits to the existing list, increasing the list’s length by the length of the new list. Also, the list is created with the help of square brackets, or we can also create it through list comprehension.

# Created a list 

fruits = ["apple", "banana", "cherry"]

# Created a new list of fruits name

new_fruits = ["orange", "kiwi", "grape"]

# Looping over the list using For Loop to add elements

for fruit in new_fruits:
    fruits.append(fruit)

# Print the updated list

print(fruits)

The output of the above program is shown below:

["apple", "banana", "cherry", "orange", "kiwi", "grape"]

Example 3: Pushing a List to Another List

In this example, you can see that we have a list of integers, and we are trying to add a new integer list to the existing list, increasing the list’s length and creating a nested list. This means we are doing list concatenation.

# Created a list 

list1 = [1, 2, 3, 4, 5]

# Created a list 

list2 = [4, 5, 6, 7, 8]

# Apply append function

list1.append(list2)

# Print the updated list

print(list1)

The output of the above program is shown below:

[1, 2, 3, 4, 5, [4, 5, 6, 7, 8]]

Example 4: Pushing a List to Another List Using the extend() Method

As in the previous example, while using the append function python, we have observed that due to the append function, we are able to add the new list completely, but not in a separate manner, i.e., each element-wise, so to implements that tasks, In this example, we have taken a list of fruits.

extend () and append ()

We are trying to add a new integer list to the existing list, increasing the list length we have to add. (list.extend)

# Created a list 

list1 = [1, 2, 3, 9, 10]

# Created a list 

list2 = [4, 5, 6, 1, 8]

# Apply to extend function instead of append

list1.extend(list2)

# Print the updated list

print(list1)

The output of the above program is shown below:

[1, 2, 3, 9, 10, 4, 5, 6, 1, 8]

Example 5: Pushing a Python Tuple to a List

In this example, we have a list of fruits and aim to add a new tuple containing fruit data to the existing list. It’s evident that besides strings, the list contains tuples of strings, which can be effortlessly appended to the end of the list.

# Created a list 

fruits = ["apple", "banana", "cherry"]

# Created a Tuple 

my_tuple = ("orange", "kiwi", "grape")

# Apply append function

fruits.append(my_tuple)

# Print the updated list

print(fruits)

The output of the above program is shown below:

["apple", "banana", "cherry", ("orange", "kiwi", "grape")]

Example 6: Pushing a Python Dictionary to a List

In the previous example, we add the tuple to an existing list. In this, we will still add one dictionary corresponding to the key value type of data structure, making the operation easy to perform. Also, observe that there are different types of elements according to the data type in the list. The indicated list can be heterogeneous, which means it can store elements of different data types.

# Created a list 

fruits = ["apple", "banana", "cherry"]

# Created a Dictionary to add 

my_dict = {"orange": 1, "kiwi": 2, "grape": 3}

# Apply append function

fruits.append(my_dict)

# Print the updated list

print(fruits)

The output of the above program is shown below:

["apple", "banana", "cherry", {"orange": 1, "kiwi": 2, "grape": 3}]

Example 7: Pushing an Integer to a List

In this example, you can see that we have a list of integers, and we are trying to add a new integer in the existing list, increasing the list’s length by one.

# Created a list 

numbers = [1, 2, 3]

# Apply append function

numbers.append(4)

# Print the updated list

print(numbers)

The output of the above program is shown below:

[1, 2, 3, 4]

Example 8: Pushing a Python String to a List

In this example, you can see that we have a list of names of people, and we are trying to add a new person’s name to the existing list, increasing the list’s length by one. Only the thing is that the element which we have to add here is the string data type.

# Created a list 

names = ["Alice", "Bob", "Charlie"]

# Apply append function

names.append("David")

# Print the updated list

print(names)

The output of the above program is shown below:

["Alice", "Bob", "Charlie", "David"]

Example 9: Pushing a Python Boolean to a List

In this example, we demonstrate adding a boolean value to an existing list of booleans, increasing its length by one. Here, you must add an element of the boolean data type, which means its value can only be True or False.

# Created a list 

flags = [True, False]

# Apply append function

flags.append(True)

# Print the updated list

print(flags)

The output of the above program is shown below:

[True, False, True]

Example 10: Pushing a None Value to a List

In this example, you can see that we have a list of integers, and we are trying to add a None value to the existing list, increasing the list’s length by one. Only the thing is that the element we have to add here is the None data type which means nothing.

# Created a list 

values = [1, 2, 3]

# Apply append function

values.append(None)

# Print the updated list

print(values)

The output of the above program is shown below:

[1, 2, 3, None]

These examples showcase how the append() method in Python is versatile and powerful, illustrating its ability to add a wide range of items to lists in various ways.

Conclusion

In conclusion, the append() method in Python programming language proves highly effective for adding new elements to an existing list. This article demonstrates its simplicity and versatility across various scenarios, enabling swift addition of items to lists, regardless of size.

To illustrate the power of the append() method, we have discussed one of the stories. We can easily append the new items to the list by writing a few program lines.

  • Regarding space complexity, we don’t have to create a new structure to store the updated list; we can utilize the initial list. In this way, we can say that this method is memory efficient.
  • Also, we can add any data type to an existing list, which means the list can work for elements of the different data types.

Therefore, the append() method in Python serves as an essential tool for quickly and easily adding new elements to your list. Overall, programmers extensively use this powerful method for working with lists in Python across various programming applications.

Frequently Asked Questions

Q1. What is append() in Python?

A. The append() method in Python adds a single element to the end of a list, modifying the original list.ecursively considering future states’ values.

Q2. What is the difference between append() and insert() method in Python?

A. append() adds an element to the end of a list, while insert() places an element at a specified index, shifting subsequent elements.

Q3. What is append and extend method in Python?

A. append() adds one element to the end of a list, whereas extend() concatenates another list or iterable to the end of the original list.

Q4. How to append in an array in Python?

A. To append to an array in Python, use the append() method for lists or numpy.append() for NumPy arrays. For example, list.append(element) or numpy.append(array, element).

I, Chirag Goyal, am a Content Editor Intern in Analytics Vidhya. I am currently pursuing my Bachelor of Technology (B.Tech) in Computer Science and Engineering from the Indian Institute of Technology Jodhpur(IITJ). I am very enthusiastic about Machine learning, Deep Learning, and Artificial Intelligence.

Responses From Readers

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details