Open In App

Python List append() Method

Last Updated : 21 Dec, 2023
Improve
Improve
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 String to list
Sometimes, while working with data, we can have a problem in which we need to add elements to a container. The list can contain any type of data type. Let's discuss certain ways in Python in which we can perform string append operations in the list of integers. Input: [1,2,3,4] Output: [1,2,3,4, 'geeksforgeeks'] Explanation: Here we appended the St
6 min read
Python | Append suffix/prefix to strings in list
Sometimes, while working with Python, we can a problem in which we need to pad strings in lists at trailing or leading position. This kind of problem is quite common and can occur in day-day programming or web development. Let's discuss a way in which this task can be performed. Method #1: Using + operator + list comprehension In this task, we just
5 min read
Python - Append Missing elements from other List
Given 2 lists, append elements missing from list 1 to list 2. Input : test_list1 = [5, 6, 4, 8, 9, 1], test_list2 = [9, 8, 10] Output : [5, 6, 4, 1, 9, 8, 10] Explanation : 5, 6, 4, 1 added to list 2, in order. Input : test_list1 = [5, 6, 4, 8, 9, 1], test_list2 = [9, 10] Output : [5, 6, 4, 8, 1, 9, 10] Explanation : 5, 6, 4, 8, 1 added to list 2,
7 min read
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
Python - Append given number with every element of the list
In Python, lists are flexible data structures that allow us to store multiple values in a single variable. Often, we may encounter situations where we need to modify each element of a list by appending a given number to it. Given a list and a number, write a Python program to append the number with every element of the list. Input: [1,2,3,4,5] Key:
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
Append Element to an Empty List In Python
Appending elements to a list is a common operation in Python, and it's essential to understand how to add elements to an empty list. In this article, we'll explore different methods to append items to an empty list and discuss their use cases. Append Element to an Empty List in PythonBelow are some examples by which we can append to an empty list i
3 min read
Python List Extend vs Append
Python, being a versatile and powerful programming language, provides multiple ways to manipulate lists. Two commonly used methods for adding elements to a list are extend and append. While both serve the purpose of adding elements, they differ in functionality and use cases. In this article, we'll delve into the nuances of Python's extend and appe
4 min read
three90RightbarBannerImg