Can you append a string to a list?

Add an item to a list in Python [append, extend, insert]

Posted: 2019-05-29 / Modified: 2021-04-06 / Tags: Python, List
Tweet

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
Sponsored Link

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.

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']
source: list_add_item.py

A list is also added as one item, not combined.

l.append[[3, 4, 5]] print[l] # [0, 1, 2, 100, 'new', [3, 4, 5]]
source: list_add_item.py

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.

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]
source: list_add_item.py

In the case of a string, each character is added one by one.

l.extend['new'] print[l] # [0, 1, 2, 100, 101, 102, -1, -2, -3, 'n', 'e', 'w']
source: list_add_item.py

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 +=.

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]
source: list_add_item.py
Sponsored Link

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.

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]
source: list_add_item.py

Like append[], the list is added as a single item, not combined.

l.insert[0, [-1, -2, -3]] print[l] # [[-1, -2, -3], 100, 0, 1, 200, 2]
source: list_add_item.py

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.

l = list[range[3]] print[l] # [0, 1, 2] l[1:1] = [100, 200, 300] print[l] # [0, 100, 200, 300, 1, 2]
source: list_add_item.py

You can also replace the original item. All items in the specified range are replaced.

l = list[range[3]] print[l] # [0, 1, 2] l[1:2] = [100, 200, 300] print[l] # [0, 100, 200, 300, 2]
source: list_add_item.py

See the following article for details of slicing.

  • How to slice a list, string, tuple in Python
Sponsored Link
Share
Tweet

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

Video liên quan

Chủ Đề