>>> x = 42
>>> xs = [1, 2, 3]
>>> xs.insert(0, x)
>>> xs
[42, 1, 2, 3]

How it works:

list.insert(index, value)

Insert an item at a given position. The first argument is the index of the element before which to insert, so xs.insert(0, x) inserts at the front of the list, and xs.insert(len(xs), x) is equivalent to xs.append(x). Negative values are treated as being relative to the end of the list.

Answer from Kousik on Stack Overflow
๐ŸŒ
DEV Community
dev.to โ€บ ra1nbow1 โ€บ 8-ways-to-add-an-element-to-the-beginning-of-a-list-and-string-in-python-925
8 ways to add an element to the beginning of a list and string in Python - DEV Community
June 3, 2022 - The insert() method takes two ... to insert an element at the beginning of the list , you need to specify 0, and not, as the first parameter 1....
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-perform-append-at-beginning-of-list
Perform Append at Beginning of List - Python - GeeksforGeeks
However, all the existing elements have to be shifted to accommodate the new one, which can make it less efficient for large lists. ... Explanation: insert(0, 6) method is used to add the value 6 at index 0, which places it at the beginning ...
Published ย  July 11, 2025
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ insert-element-at-the-beginning-of-an-array
Insert Element at the Beginning of an Array - GeeksforGeeks
November 7, 2024 - Time Complexity: O(n), where n is the size of the array. To insert an element at the beginning of an array, first shift all the elements of the array to the right by 1 index and after shifting insert the new element at 0th position.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: append() to add values to an array | note.nkmk.me
February 4, 2024 - In NumPy, the np.append() function allows you to add values (elements, rows, or columns) to either the end or the beginning of an array (ndarray).
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-add-to-array
Python Array Add: How to Append, Extend & Insert Elements | DigitalOcean
April 15, 2025 - Learn how to add elements to an array in Python using append(), extend(), insert(), and NumPy functions. Compare performance and avoid common errors.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ list โ€บ insert
Python List insert()
# create a list of vowels vowel = ['a', 'e', 'i', 'u'] # 'o' is inserted at index 3 (4th position) vowel.insert(3, 'o') print('List:', vowel) # Output: List: ['a', 'e', 'i', 'o', 'u'] ... Here, elem is inserted to the list at the ith index. All the elements after elem are shifted to the right. ... If index is 0, the element is inserted at the beginning of the list.
Find elsewhere
๐ŸŒ
Machinelearninghelp
machinelearninghelp.org โ€บ programming-for-machine-learning โ€บ how-to-add-element-to-beginning-of-array-python
Adding Elements to the Beginning of an Array in Python
Here, we use np.insert() to add a new element at the beginning of our NumPy array. This approach is generally more memory-efficient for large datasets. While not directly applicable in this context, understanding how arrays work under the hood can enhance your programming skills. In Python, lists (arrays) are implemented as dynamic arrays, which means they store elements in a contiguous block of memory.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third-party โ€บ numpy โ€บ insert
Python Numpy insert() - Insert Elements | Vultr Docs
November 15, 2024 - Explore various examples that ... of your original data array. Import the NumPy library. Define an initial array. Use numpy.insert() to add an element at a desired index....
๐ŸŒ
Index.dev
index.dev โ€บ blog โ€บ prepend-an-element-to-a-short-python-list
How to Prepend an Element to a Short Python List
The deque class from the collections module provides an appendleft() method that efficiently adds an element to the beginning of the deque. Converting the deque back to a list with list(my_deque) gives you a regular Python list.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python insert into list
How to Append to Front of a List in Python | Delft Stack
February 20, 2025 - Inserting an element at the beginning of a Python list is a common operation that can be efficiently accomplished using the listโ€™s insert() method. This method inserts an element to the first index in the list, and by specifying the index ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_array_add.asp
Python Add Array Item
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... You can use the append() method to add an ...
๐ŸŒ
Centron
centron.de โ€บ startseite โ€บ how to add elements to an array in python
How To Add Elements to an Array in Python โ€“ Step-by-Step Guide
December 6, 2024 - Learn how to add elements to an array in Python with array and NumPy modules. Simple examples for efficient array manipulation.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.1 โ€บ reference โ€บ generated โ€บ numpy.insert.html
numpy.insert โ€” NumPy v2.1 Manual
A copy of arr with values inserted. Note that insert does not occur in-place: a new array is returned. If axis is None, out is a flattened array. ... Append elements at the end of an array.
๐ŸŒ
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.
๐ŸŒ
Spark By {Examples}
sparkbyexamples.com โ€บ home โ€บ python โ€บ add element to front of list in python
Add Element to Front of List in Python - Spark By {Examples}
May 31, 2024 - This code creates a deque from the original list, adds the element 'Hyperion' to the front of the deque using appendleft(), and then converts the deque back to a list for display purposes. from collections import deque # Consider the list of strings technology = ['Spark', 'Python','Pandas'] print("Actual List: ",technology) # Using collections.deque.pushleft() # Add element to front of list technology= deque(technology) technology.appendleft('Hyperion') technology = list(technology) print("Final List: ",technology) # Output: # Actual List: ['Spark', 'Python', 'Pandas'] # Final List: ['Hyperion', 'Spark', 'Python', 'Pandas']