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 with append() and insert(). Combining lists can be done with extend(), +, +=, and slicing.

To learn how to remove an item from a list, see the following article:

Add an item to a list: append()

The append() method allows you to add a single item to the end of a list. To insert an item at a different position, such as the beginning, use the insert() method described later.

l = [0, 1, 2]

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

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

When adding a list with append(), the list is added as a single item instead of being merged with the original list.

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

Combine lists: extend(), +, +=

You can use the extend() method to combine lists, adding all items from one list to the end of another. Other iterable objects, such as tuples, can also be used with extend().

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]

When using a string (str) with extend(), each character is added to the list individually.

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 creates a new list by concatenating two lists, while the += operator adds the elements of one list to the end of the existing one.

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]

Insert an item into a list: insert()

The insert() method allows you to add an item at any specified index (position) within a list.

Set the first argument as the index and the second argument as the item to be inserted. The index starts at 0 (zero-based indexing). Negative values, like -1, refer to positions counting from the end of the list.

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']

Similar to append(), when using insert() with a list, the list is added as a single item instead of being combined with the original list.

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

Note that insert() has an O(n) time complexity and can be inefficient. Refer to the official Python wiki for the computational complexity of various list operations:

The deque type, provided in the collections module, allows adding an item to the head of a list with O(1) time complexity. For example, when treating data as a queue (FIFO), deque is a more efficient option.

Insert a list into another list: slicing

You can insert a list or tuple into another list by specifying a range using slicing and assigning the new list or tuple to that range. All items from the new list or tuple will be added.

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

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

You can also replace items in the original list with items from the new list or tuple by specifying a range using slicing.

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

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

See the following article for details on slicing.

Related Categories

Related Articles