The Wayback Machine - https://web.archive.org/web/20240717024531/https://www.geeksforgeeks.org/python-list-append-method/
Open In App

Python List append() Method

Last Updated : 21 Dec, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Python list append() method is used to add elements at the end of the list.

Example

Python




list=[2,5,6,7]
list.append(8)
print(list)


Output

[2, 5, 6, 7, 8]

Python List Append() Syntax

List_name.append(element)

Parameter

  • element: an element (number, string, list, etc.) to be added at the end of the list. The parameter is mandatory and omitting it can cause an error.

What is List Append() in Python?

Python list append function is a pre-defined function that takes a value as a parameter and adds it at the end of the list. append() function can take any type of data as input, including a number, a string, a decimal number, a list, or another object.

How to use list append() method in Python?

It is very easy to use function you just need to call function with list object and passing value as parameter.

Adding elements to the end of a list with Python’s append() method increases the list’s size. It offers a practical method to add one or more elements to an existing list. Here is an example of using the List Append() method.

Python List append() Method

More Python List append() Examples of

Here are some examples and use-cases of list append() function in Python.

Append to list in Python using List.append() Method

List.append() method is used to add an element to the end of a list in Python or append a list in Python, modifying the list in place. For example: `my_list.append(5)` adds the element `5` to the end of the list `my_list`.

Example: In this example, the In below code Python list append() adds a new element at the end of list.

Python




# using list fruits
fruits = ["apple", "cherry", "banana"]
  
# adding new element grapes
fruits.append("grapes")
  
# priting the fruits list
print(fruits)


Output :

['apple', 'cherry', 'banana', 'grapes']

Add List to the List using Python List Append() Method

This method involves appending a list to another list in Python using the `append()` method, thereby adding the entire list as a single element to the target list.

Example: In this example, we will append a complete list at the end of the given list.

Python




# my_list
my_list = ['geeks', 'for', 'geeks']
  
# another list
another_list = [6, 0, 4, 1]
  
# append another_list to my_list
my_list.append(another_list)
  
print(my_list)


Output:

['geeks', 'for', 'geeks', [6, 0, 4, 1]]

Note: A list is an object. If you append another list onto a list, the parameter list will be a single object at the end of the list. If you want to append the elements of a list to another list you can use Python’s List extend() method.
Time complexity: O(1) or constant time. This is because lists can be accessed randomly using indexing, so the last element can be accessed easily in O(1) time.

We have discussed the definition, syntax, and examples of append() in Python. Append function is a very basic and useful operation in Python programming and should be known by every Python student.

Read Other Python List Methods

Similar Reads:



Previous Article
Next Article

Similar Reads

Python - Append List every Nth index
Given 2 list, append list to the original list every nth index. Input : test_list = [3, 7, 8, 2, 1, 5, 8], app_list = ['G', 'F', 'G'], N = 3 Output : ['G', 'F', 'G', 3, 7, 8, 'G', 'F', 'G', 2, 1, 5, 'G', 'F', 'G', 8] Explanation : List is added after every 3rd element.Input : test_list = [3, 7, 8, 2, 1, 5, 8, 9], app_list = ['G', 'F', 'G'], N = 4 O
5 min read
How to append a list as a row to a Pandas DataFrame in Python?
Prerequisite: Pandas DataFrame In this article, We are going to see how to append a list as a row to a pandas dataframe in Python. It can be done in three ways: Using loc[]Using iloc[]Using append()Append list using loc[] methods Pandas DataFrame.loc attribute access a group of rows and columns by label(s) or a boolean array in the given DataFrame.
3 min read
Append list of dictionary and series to a existing Pandas DataFrame in Python
In this article, we will discuss how values from a list of dictionaries or Pandas Series can be appended to an already existing pandas dataframe. For this purpose append() function of pandas, the module is sufficient. Syntax: DataFrame.append(other, ignore_index=False, verify_integrity=False, sort=None) Parameters :other : DataFrame or Series/dict-
2 min read
Python | Perform append at beginning of list
The usual append operation of a Python list adds the new element at the end of the list. But in certain situations, we need to append each element to the front of the list. If we perform brute force techniques, we need to perform unnecessary shifts of elements and hence, having shorthand for it is useful. Here, we will see how to add to a list in P
6 min read
Future Replacement of the Append Method in Panda Python
In The Current Version of Panda ['1.5.3'], if you use the append () function your code will get executed but In Jupyter Notebook you will receive some sort of error or can say Future Warning. Let's First execute some series code in pandas Python: Replacement of the Append Method in PandasBelow are the methods that we will cover in this article: Pro
2 min read
append() and extend() in Python
Extend and Append are two Python list methods used to add elements to a list. Although they appear similar, they have different functionalities and use cases. Understanding the differences between the append() and extend() methods is crucial when working with lists in Python. Although both techniques are used to add elements to a list, their behavi
4 min read
Python | Pandas Index.append()
Python is an excellent language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas are one of those packages, making importing and analyzing data much easier. Pandas Index.append() The function is used to append a single or a collection of indices together. In the case of a collection of indices,
2 min read
Python | Pandas TimedeltaIndex.append()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.append() function appends a collection of index options together. More than one index object can be appended at a
2 min read
Python | Pandas Series.append()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.append() function is used to concatenate two or more series object. Syntax: Ser
4 min read
Python append to a file
While reading or writing to a file, access mode governs the type of operations possible in the opened file. It refers to how the file will be used once it's opened. These modes also define the location of the File Handle in the file. The definition of these access modes is as follows: Append Only (‘a’): Open the file for writing.Append and Read (‘a
4 min read
three90RightbarBannerImg