To add an element to a list (array) of type list in Python, or to combine another list, use the list methods append(), extend(), and insert(). You can also use the + operator or slice to specify a position and assign it.
The following information is provided here.
- Add elements at the end:
append()
- Merge another list or tuple at the end (concatenation):
extend()
,+
operator - Add (insert) an element at the specified position.:
insert()
- Add (insert) another list or tuple at the specified position:slice
Add elements at the end: append()
Using the append() method of the list, you can add elements to the end (last). If you want to add it to a position other than the end, such as the top, use insert() as described below.
l = list(range(3))
print(l)
# [0, 1, 2]
l.append(100)
print(l)
# [0, 1, 2, 100]
l.append('new')
print(l)
# [0, 1, 2, 100, 'new']
Lists are also added as a single element. They are not combined.
l.append([3, 4, 5])
print(l)
# [0, 1, 2, 100, 'new', [3, 4, 5]]
Merge another list or tuple at the end (concatenation): extend(), +operator
With the list method extend(), you can combine another list or tuple at the end (the end). All elements will be appended to the end of the original list.
l = list(range(3))
print(l)
# [0, 1, 2]
l.extend([100, 101, 102])
print(l)
# [0, 1, 2, 100, 101, 102]
l.extend((-1, -2, -3))
print(l)
# [0, 1, 2, 100, 101, 102, -1, -2, -3]
Note that each character (element) is added to the string one character at a time.
l.extend('new')
print(l)
# [0, 1, 2, 100, 101, 102, -1, -2, -3, 'n', 'e', 'w']
It is also possible to concatenate using the + operator instead of the extend() method.
+ operator, a new list is returned.+=
This will also allow you to add it to an existing list.
l2 = l + [5, 6, 7]
print(l2)
# [0, 1, 2, 100, 101, 102, -1, -2, -3, 'n', 'e', 'w', 5, 6, 7]
l += [5, 6, 7]
print(l)
# [0, 1, 2, 100, 101, 102, -1, -2, -3, 'n', 'e', 'w', 5, 6, 7]
Add (insert) an element at the specified position.: insert()
The list method insert() can add (insert) an element at a specified position.
The first argument specifies the position, and the second argument specifies the element to be inserted. The first (initial) position is 0; for negative values, -1 is the last (final) position.
l = list(range(3))
print(l)
# [0, 1, 2]
l.insert(0, 100)
print(l)
# [100, 0, 1, 2]
l.insert(-1, 200)
print(l)
# [100, 0, 1, 200, 2]
As with append(), the list is added as a single element. It will not be merged.
l.insert(0, [-1, -2, -3])
print(l)
# [[-1, -2, -3], 100, 0, 1, 200, 2]
Note that insert() is not an efficient operation because it requires the following costs. See the following page on the official wiki for the computational complexity of the various list operations.O(n)
O(1)
The deque type is provided in the standard library collections module as a type to add elements to the top at this cost. For example, if you want to treat data as a queue (FIFO), it is more efficient to use deque.
Add (insert) another list or tuple at the specified position: slice
If you specify a range with a slice and assign another list or tuple, all the elements will be added (inserted).
l = list(range(3))
print(l)
# [0, 1, 2]
l[1:1] = [100, 200, 300]
print(l)
# [0, 100, 200, 300, 1, 2]
You can also replace the original element. All elements in the specified range will be replaced.
l = list(range(3))
print(l)
# [0, 1, 2]
l[1:2] = [100, 200, 300]
print(l)
# [0, 100, 200, 300, 2]