Appending an Element to a Python List

In Python, you can append an element to a list using the list.append() or insert an element into a list using the list. insert() or list.extend() methods. The list.append() method appends an element to the end of the list. The list.insert() method inserts an element at the specified index (position). Indexing starts from zero, and a negative index of -1 means that the element will be added to the index starting from the end of the list. If you want to concatenate multiple lists, use the "+" operator. You can add elements of different types to the list (numbers, strings, etc.). In this Python List Append Example, we use the list.append() method to add an item to the end of the list. Additional examples of adding items to the list are provided below with detailed descriptions. Click Execute to run the Python List Append Example online and see the result.
Appending an Element to a Python List Execute
newlist = ["orange", "grape", "mango"]

newlist.append("pineapple")

print(newlist)
Updated: Viewed: 4793 times

What is the List in Python?

Python Lists are data structures that contain a disordered sequence of elements. Elements of lists can be of various types, including int, float, string, a custom class, and even another list. Python lists are mutable, meaning they can be modified by adding elements, removing them, or sorting. Adding or removing items from the list will change the size of the list, which is not fixed. To create a list and initialize it with values, you can enclose them in square brackets, separated by commas.

Create and initialize Python list
my_list = [0, 3, 2, 1]

print(my_list)

# [0, 3, 2, 1]

How to append an item to the end of the list?

To append item to the end of the list, you can use the list.append() method:

Python List Append Example
newlist = ["orange", "grape", "mango"]

newlist.append("pineapple")
          
print(newlist)

# ['orange', 'grape', 'mango', 'pineapple']

How to insert an item to a specific position in the list?

To insert an element at a specified position in a list, you can use the list.insert() method and pass the position as the first argument and the element itself as the second argument:

Python List Insert Example
newlist = ["orange", "grape", "mango"]

newlist.insert(1, "pineapple")
          
print(newlist)

# ['orange', 'pineapple', 'grape', 'mango']

How to append items from another list to the end of the list?

You can use the list.extend() method to add all elements from another list to the end of your list:

Python List Extend Example
newlist = ["apple", "banana", "cherry"]
nutslist = ["almond", "peanut", "pistachio"]

newlist.extend(nutslist)

print(newlist)

# ['apple', 'banana', 'cherry', 'almond', 'peanut', 'pistachio']

How to concatenate multiple lists?

To concatenate two lists, you can use the "+" operator. The new list will contain items from the list from left to right. This is similar to string concatenation in Python.

Python List Concatenation Example
odds = [1, 3, 5]
evens = [2, 4, 6]
          
a = odds + evens
          
print(a)

# [1, 3, 5, 2, 4, 6]

See also