Add an item to a list in Python [append, extend, insert]
In Python, use list methods append[], extend[], and insert[] to add items [elements] to a list or combine other lists. You can also use the + operator to combine lists, or use slices to insert items at specific positions.
- Add an item to the end: append[]
- Combine lists: extend[], + operator
- Insert an item at specified index: insert[]
- Add another list or tuple at specified index: slice
Add an item to the end: append[]
You can add an item to the end of the list with append[]. If you want to add to positions other than the end, such as the beginning, use insert[] described later.
A list is also added as one item, not combined.
Combine lists: extend[], + operator
You can combine another list or tuple at the end of the list with extend[]. All items are added to the end of the original list.
In the case of a string, each character is added one by one.
It is also possible to combine using the + operator instead of extend[].
In the case of the + operator, a new list is returned. You can also add another list to the existing list with +=.
Insert an item at specified index: insert[]
You can insert an item at the specified index [position] by insert[].
Set the index for the first parameter and the item to be inserted for the second parameter. The index at the beginning is 0 [zero-based indexing]. For negative values, -1 means one before the end.
Like append[], the list is added as a single item, not combined.
Note that insert[] is an O[n] operation and is not efficient. See the official wiki for the computational complexity of various operations on list.
- TimeComplexity - Python Wiki
The deque type is provided in the standard library collections module as a type to add an item to the head with O[1]. For example, if you want to treat data as a queue [FIFO], it is more efficient to use deque.
- Queue, stack, and deque [double-ended queue] in Python
Add another list or tuple at specified index: slice
If you specify a range using slice and assign another list or tuple, all items will be added.
You can also replace the original item. All items in the specified range are replaced.
See the following article for details of slicing.
- How to slice a list, string, tuple in Python
Related Categories
- Python
- List
Related Articles
- Get the number of items of a list in Python
- Swap values in a list or values of variables in Python
- Filter [extract / remove] items of a list with filter[] in Python
- Apply a function to items of a list with map[] in Python
- Extract, replace, convert elements of a list in Python
- Convert 1D array to 2D array in Python [numpy.ndarray, list]
- in operator in Python [for list, string, dictionary, etc.]
- Remove / extract duplicate elements from list in Python
- Random sampling from a list in Python [random.choice, sample, choices]
- How to slice a list, string, tuple in Python
- Extract and replace elements that meet the conditions of a list of strings in Python
- Unpack and pass list, tuple, dict to function arguments in Python
- Unpack a tuple / list in Python
- Convert numpy.ndarray and list to each other
- Queue, stack, and deque [double-ended queue] in Python