You can use list indexes:

>>> mylist = [['Bob\n', 0], ['Joe\n', 0], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
>>> mylist[1][1] = 'new_value'
>>> mylist
[['Bob\n', 0], ['Joe\n', 'new_value'], ['Bill', 0], ['Steve', 0], ['Judy', 0]]

In mylist, in 2nd element (1), in 2nd element of sublist, I set 'new_value'

And for \n, you can use str.strip:

>>> mylist = [['Bob\n', 0], ['Joe\n', 0], ['Bill', 0], ['Steve', 0], ['Judy', 0]]
>>> mylist = [[i.strip(), j] for i, j in mylist]
>>> mylist
[['Bob', 0], ['Joe', 0], ['Bill', 0], ['Steve', 0], ['Judy', 0]]

Use a list-comprehension and apply strip to 1st element of all sublists.

Answer from Zulu on Stack Overflow
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-add-to-array
Python Array Add: How to Append, Extend & Insert Elements | DigitalOcean
April 15, 2025 - OutputTraceback (most recent call last): File "<stdin>", line 1, in <module> File "<__array_function__ internals>", line 5, in append File "/Users/digitalocean/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/function_base.py", line 4817, in append return concatenate((arr, values), axis=axis) File "<__array_function__ internals>", line 5, in concatenate ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 2 and the array at index 1 has size 3
🌐
AskPython
askpython.com › home › python add elements to an array
Python add elements to an Array - AskPython
January 16, 2024 - my_input = [1, 2, 3, 4, 5] ... function: It adds elements to the end of the array. By using insert() function: It inserts the elements at the given index....
Top answer
1 of 6
141

l.insert(index, obj) doesn't actually return anything. It just updates the list.

As ATO said, you can do b = a[:index] + [obj] + a[index:]. However, another way is:

a = [1, 2, 4]
b = a[:]
b.insert(2, 3)
2 of 6
111

Most performance efficient approach

You may also insert the element using the slice indexing in the list. For example:

>>> a = [1, 2, 4]
>>> insert_at = 2  # Index at which you want to insert item

>>> b = a[:]   # Created copy of list "a" as "b".
               # Skip this step if you are ok with modifying the original list

>>> b[insert_at:insert_at] = [3]  # Insert "3" within "b"
>>> b
[1, 2, 3, 4]

For inserting multiple elements together at a given index, all you need to do is to use a list of multiple elements that you want to insert. For example:

>>> a = [1, 2, 4]
>>> insert_at = 2   # Index starting from which multiple elements will be inserted

# List of elements that you want to insert together at "index_at" (above) position
>>> insert_elements = [3, 5, 6]

>>> a[insert_at:insert_at] = insert_elements
>>> a   # [3, 5, 6] are inserted together in `a` starting at index "2"
[1, 2, 3, 5, 6, 4]

To know more about slice indexing, you can refer: Understanding slice notation.

Note: In Python 3.x, difference of performance between slice indexing and list.index(...) is significantly reduced and both are almost equivalent. However, in Python 2.x, this difference is quite noticeable. I have shared performance comparisons later in this answer.


Alternative using list comprehension (but very slow in terms of performance):

As an alternative, it can be achieved using list comprehension with enumerate too. (But please don't do it this way. It is just for illustration):

>>> a = [1, 2, 4]
>>> insert_at = 2

>>> b = [y for i, x in enumerate(a) for y in ((3, x) if i == insert_at else (x, ))]
>>> b
[1, 2, 3, 4]

Performance comparison of all solutions

Here's the timeit comparison of all the answers with list of 1000 elements on Python 3.9.1 and Python 2.7.16. Answers are listed in the order of performance for both the Python versions.

Python 3.9.1

  1. My answer using sliced insertion - Fastest ( 2.25 µsec per loop)

    python3 -m timeit -s "a = list(range(1000))" "b = a[:]; b[500:500] = [3]"
    100000 loops, best of 5: 2.25 µsec per loop
    
  2. Rushy Panchal's answer with most votes using list.insert(...)- Second (2.33 µsec per loop)

    python3 -m timeit -s "a = list(range(1000))" "b = a[:]; b.insert(500, 3)"
    100000 loops, best of 5: 2.33 µsec per loop
    
  3. ATOzTOA's accepted answer based on merge of sliced lists - Third (5.01 µsec per loop)

    python3 -m timeit -s "a = list(range(1000))" "b = a[:500] + [3] + a[500:]"
    50000 loops, best of 5: 5.01 µsec per loop
    
  4. My answer with List Comprehension and enumerate - Fourth (very slow with 135 µsec per loop)

    python3 -m timeit -s "a = list(range(1000))" "[y for i, x in enumerate(a) for y in ((3, x) if i == 500 else (x, )) ]"
    2000 loops, best of 5: 135 µsec per loop
    

Python 2.7.16

  1. My answer using sliced insertion - Fastest (2.09 µsec per loop)

    python -m timeit -s "a = list(range(1000))" "b = a[:]; b[500:500] = [3]"
    100000 loops, best of 3: 2.09 µsec per loop
    
  2. Rushy Panchal's answer with most votes using list.insert(...)- Second (2.36 µsec per loop)

    python -m timeit -s "a = list(range(1000))" "b = a[:]; b.insert(500, 3)"
    100000 loops, best of 3: 2.36 µsec per loop
    
  3. ATOzTOA's accepted answer based on merge of sliced lists - Third (4.44 µsec per loop)

    python -m timeit -s "a = list(range(1000))" "b = a[:500] + [3] + a[500:]"
    100000 loops, best of 3: 4.44 µsec per loop
    
  4. My answer with List Comprehension and enumerate - Fourth (very slow with 103 µsec per loop)

    python -m timeit -s "a = list(range(1000))" "[y for i, x in enumerate(a) for y in ((3, x) if i == 500 else (x, )) ]"
    10000 loops, best of 3: 103 µsec per loop
    
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.insert.html
numpy.insert — NumPy v2.4 Manual
Append elements at the end of an array. concatenate · Join a sequence of arrays along an existing axis. delete · Delete elements from an array. Notes · Note that for higher dimensional inserts obj=0 behaves very different from obj=[0] just like arr[:,0,:] = values is different from arr[:,[0],:] = values. This is because of the difference between basic and advanced indexing.
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › add elements to python array
Add Elements to Python Array (3 Methods)
December 15, 2025 - The old list extends with the new elements, which all append to the end of the list. 4. Add an element to an exact location in a list with the insert() method. Provide the location index and the value to insert.
🌐
Hostman
hostman.com › tutorials › how to add elements to an array in python
Python: How to Add Elements to an Array
March 31, 2025 - It successfully appended 102 at the first index of AuthorsIDs: When managing arrays in Python, the append() method is utilized to insert a single value to the final index of the array.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
Programiz
programiz.com › python-programming › methods › list › insert
Python List insert()
Become a certified Python programmer. Try Programiz PRO! ... The insert() method inserts an element to the list at the specified index.
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › add element to list by index in python
Add Element to List by Index in Python - Spark By {Examples}
May 31, 2024 - If you want to add an element at the last index position of the python list, you can use the len() to find the total number of elements in the list and use this as an index to the along with the element you wanted to add.
🌐
W3Schools
w3schools.com › python › python_arrays.asp
Python Arrays
You can use the for in loop to loop through all the elements of an array. ... You can use the append() method to add an element to an array. ... You can use the pop() method to remove an element from the array.
🌐
Real Python
realpython.com › python-append
Python's .append(): Add Items to Your Lists in Place – Real Python
October 21, 2023 - Arrays support most list operations, such as slicing and indexing. Like lists, array.array() also provides a method called .append(). This method works similarly to its list counterpart, adding a single value to the end of the underlying array.
🌐
freeCodeCamp
freecodecamp.org › news › append-in-python-how-to-append-to-a-list-or-an-array
Append in Python – How to Append to a List or an Array
January 7, 2022 - It's always an integer - specifically it's the index number of the position where you want the new item to be placed. item is the second argument to the method. Here you specify the new item you want to add to the list.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.append.html
numpy.append — NumPy v2.4 Manual
>>> np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0) Traceback (most recent call last): ... ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s) >>> a = np.array([1, 2], dtype=int) >>> c = np.append(a, []) >>> c array([1., 2.]) >>> c.dtype float64 ·
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › add elements to an array in python
Add Elements to an Array in Python - Spark By {Examples}
May 31, 2024 - You can use the insert() to add an element at a specific index in the Python list. For example, you can insert 'Pandas' it at the first position on the list.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-add-to-list
How to Add Elements to a List in Python – Append, Insert & Extend | DigitalOcean
April 17, 2025 - There are four methods to add elements to a List in Python. append(): append the element to the end of the list. insert(): inserts the element before the given index.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.append.html
numpy.append — NumPy v2.2 Manual
>>> np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0) Traceback (most recent call last): ... ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s) >>> a = np.array([1, 2], dtype=int) >>> c = np.append(a, []) >>> c array([1., 2.]) >>> c.dtype float64 ·
🌐
Python.org
discuss.python.org › python help
List.append questions for indexing? - Python Help - Discussions on Python.org
December 3, 2022 - My data comes originally from a tuple of twenty one variables. There are several tuples to be added one at a time, each with twenty one variables. Each tuple is changed to a list, and then appended to a file. My question is with multiple tuples appended to a file (now containing one long list), ...
🌐
freeCodeCamp
freecodecamp.org › news › python-list-append-how-to-add-an-element-to-an-array-explained-with-examples
Python List Append – How to Add an Element to an Array, Explained with Examples
May 8, 2020 - >>> data = [{"a": 1, "b": 2}] >>> ... to append() if we pass the correct arguments. The insert() method is used to insert an element at a particular index (position) in the list....