Add an Item to a List in Python: append, extend, insert

Modified: | Tags: Python, List

In Python, you can add a single item (element) to a list using append() and insert(). You can combine lists using extend(), +, +=, and slicing.

For details on removing an item from a list, refer to the following article:

Add an item to a list: append()

The append() method adds a single item to the end of a list. To add an item at a specific position, such as the beginning, use the insert() method (explained below).

l = [0, 1, 2]

l.append(100)
print(l)
# [0, 1, 2, 100]

l.append('abc')
print(l)
# [0, 1, 2, 100, 'abc']

If a list is passed to append(), it is added as a single element, not merged with the existing list.

l.append([3, 4, 5])
print(l)
# [0, 1, 2, 100, 'abc', [3, 4, 5]]

Combine lists: extend(), +, +=

The extend() method appends all items from another iterable, such as a list or tuple, to the end of the list.

l = [0, 1, 2]

l.extend([10, 11, 12])
print(l)
# [0, 1, 2, 10, 11, 12]

l.extend((100, 101, 102))
print(l)
# [0, 1, 2, 10, 11, 12, 100, 101, 102]

Note that when using a string (str) with extend(), each character in the string is added as a separate element.

l.extend('abc')
print(l)
# [0, 1, 2, 10, 11, 12, 100, 101, 102, 'a', 'b', 'c']

Lists can also be combined using the + and += operators.

The + operator returns a new list by concatenating two lists, while += modifies the original list in place by appending items from the other.

l_new = l + [3, 4, 5]
print(l_new)
# [0, 1, 2, 10, 11, 12, 100, 101, 102, 'a', 'b', 'c', 3, 4, 5]

l += [3, 4, 5]
print(l)
# [0, 1, 2, 10, 11, 12, 100, 101, 102, 'a', 'b', 'c', 3, 4, 5]

Add an item at a specific position: insert()

The insert() method places an item at a specified index within the list.

The first argument is the index, and the second is the item to insert. Indexing starts at 0; negative values like -1 count from the end.

l = ['a', 'b', 'c']

l.insert(1, 100)
print(l)
# ['a', 100, 'b', 'c']

l.insert(0, 200)
print(l)
# [200, 'a', 100, 'b', 'c']

l.insert(-1, 300)
print(l)
# [200, 'a', 100, 'b', 300, 'c']

l.insert(-2, 400)
print(l)
# [200, 'a', 100, 'b', 400, 300, 'c']

As with append(), passing a list to insert() adds it as a single element, rather than combining its contents with the target list.

l.insert(0, [-1, -2, -3])
print(l)
# [[-1, -2, -3], 200, 'a', 100, 'b', 400, 300, 'c']

Keep in mind that insert() has O(n) time complexity, which may be inefficient for large lists. Refer to the official Python wiki for the computational complexity of various list operations:

To efficiently add an item to the beginning of a list, consider using deque from the collections module, which supports O(1) insertion at both ends. This is particularly useful when implementing a queue (FIFO).

Insert a list into another list: slicing

Slicing can be used to insert a list or tuple into another list.

By assigning a list or tuple to a slice, its items are inserted into the specified range.

l = ['a', 'b', 'c']

l[1:1] = [100, 200, 300]
print(l)
# ['a', 100, 200, 300, 'b', 'c']

This technique can also replace items in the original list with those from the new sequence.

l = ['a', 'b', 'c']

l[1:2] = [100, 200, 300]
print(l)
# ['a', 100, 200, 300, 'c']

For more details on slicing, refer to the following article:

Related Categories

Related Articles