A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-list-append-method/ below:

Python List append() Method - GeeksforGeeks

Python List append() Method

Last Updated : 18 Mar, 2025

append() method in Python is used to add a single item to the end of list. This method modifies the original list and does not return a new list. Let's look at an example to better understand this.

Python
a = [2, 5, 6, 7]

# Use append() to add the element 8 to the end of the list
a.append(8)
print(a)

Explanation: append(8) adds 8 to the end of the list a, modifying it in place.

Syntax of append() method

list.append(element)

Parameter: Return:

Examples of append() Method

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

1. Appending Elements of Different Types

The append() method allows adding elements of different data types (integers, strings, lists or objects) to a list. Python lists are heterogeneous meaning they can hold a mix of data types.

Python
a = [1, "hello", 3.14]

a.append(True)
print(a)

Output
[1, 'hello', 3.14, True]

Explanation: In this list "a" contains elements of different data types (integer, string, float) and append(True) adds a boolean True to the end of the list.

2. Appending List to a List

When appending one list to another, the entire list is added as a single element, creating a nested list.

Python
a = [1, 2, 3]

a.append([4, 5])
print(a)

Explanation: append() method adds the list [4, 5] as a single element to the end of the list a, resulting in a nested list.

3. Appending Using a Loop

Appending multiple elements using a loop:

Python
a = []
for i in range(5):
    a.append(i)
print(a)
Append vs Extend vs Insert

Method

Functionality

append(x)

Adds x as a single element at the end of the list.

extend(iterable)

Adds all elements of iterable individually to the list.

insert(index, x)

Inserts x at the specified index.

Example demonstrating the difference between append and extend:

Python
a = [1, 2, 3]
a.append([4, 5])  
print(a)  

b = [1, 2, 3]
b.extend([4, 5])  
print(b)  

Output
[1, 2, 3, [4, 5]]
[1, 2, 3, 4, 5]

Explanation:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4