Prepending means adding an element at the start of the list. If I have a list like [2, 3, 4] and I want to prepend element 1, it becomes [1, 2, 3, 4].
Here are five ways to prepend to a list in Python:
- Using insert()
- Using list slicing
- Using concatenation (+ operator)
- Using collections.deque.appendleft()
- Using list unpacking
Method 1: Using insert()
The list.insert() method allows us to insert an item at any position in the list. To prepend, insert at position 0 using the snippet insert(0, element). It adds an element at the 0th index.
The insert() method modifies the list in place, meaning it does not return a new list.
In the above figure, you can see that we prepended element “3” at the start of the list “sample_list”. The index() method shifted all the other elements to the right after prepending.
# Define a list with some elements sample_list = [6, 9, 12, 15] print("Before Prepending:", sample_list) # The insert() method adds '3' at the index 0 sample_list.insert(0, 3) print("After Prepending:", sample_list) # Before Prepending: [6, 9, 12, 15] # After Prepending: [3, 6, 9, 12, 15]
Pros
- It works with all Python versions. (No version restrictions).
- It has a simple syntax which is easy for beginners.
Cons
- It shifts existing elements to the right side, which takes time. That’s why time complexity is O(n).
- It is inefficient for large lists.
- If you want to prepend multiple elements, it requires a loop, which makes the process slow.
Method 2: Using list slicing
The list[:0] = [element] specifies the position before the first element of the list. By assigning [3] to this slice, you are effectively inserting 3 at the start of the list. It alters the original list because it’s in place modification.
The above illustration shows that we are prepending a list with element “3” directly to the original list sample_list at the index 0.
sample_list = [6, 9, 12, 15] print("Before Prepending:", sample_list) sample_list[:0] = [3] # Prepend element 3 to the list print("After Prepending:", sample_list) # Before Prepending: [6, 9, 12, 15] # After Prepending: [3, 6, 9, 12, 15]
Prepending multiple elements
Using list slicing, you can prepend multiple elements at once. Simply assign a list of new elements to the 0th index of the original list with slicing, and they will be prepended.
sample_list = [6, 9, 12, 15] print("Before Prepending:", sample_list) sample_list[:0] = [3, 4, 5] # Prepend multiple lists print("After Prepending:", sample_list) # Before Prepending: [6, 9, 12, 15] # After Prepending: [3, 4, 5, 6, 9, 12, 15]
The above commented output shows that elements 3, 4, and 5 have been prepended to the original list.
Pros
- You can easily prepend multiple elements.
Cons
- It has a less intuitive syntax.
- Since it shifts all elements, it is inefficient for large lists. And time complexity is O(n).
Method 3: Using concatenation (+ operator)
You can use square brackets [] and the + operator to create a new list with the item and concatenate it with the original list. The syntax is simple: [element] + list. It will return a new list and does not modify the original one.
The above visual representation shows that we prepended element 3 using the concatenate operator. The output is a new list called “my_list”.
sample_list = [6, 9, 12, 15] print("Before Prepending:", sample_list) my_list = [3] + sample_list # prepending element 3 to the list # Output is the new list print("After Prepending:", my_list) # Before Prepending: [6, 9, 12, 15] # After Prepending: [3, 6, 9, 12, 15]
Prepending multiple elements
In this approach, you can easily prepend multiple elements by putting a list of new elements on the left side of the “+” operator. If you are operating in a functional programming environment, then this can be a better approach.
sample_list = [6, 9, 12, 15] print("Before Prepending multiple elements:", sample_list) # prepending multiple elements multiple_elements_list = [3, 4, 5] + sample_list # Output is the new list print("After Prepending multiple elements:", multiple_elements_list) # Before Prepending multiple elements: [6, 9, 12, 15] # After Prepending multiple elements: [3, 4, 5, 6, 9, 12, 15]
The above output shows that we prepended elements 3, 4, and 5 and returned a new list.
Pros
- This approach clearly expressed its intent by putting a list of elements to the front.
- If you want to preserve the original list, this approach is suitable for you because it does not alter the original list. You can think of it as pure operation.
- It is a simple method for single or multiple elements.
Cons
- Since it creates a new list, it is memory inefficient, and its time and space complexity is O(n).
Method 4: Using collections.deque.appendleft()
For very large lists, consider using collections.deque for efficient appends and prepends. If you strictly require a list, then you should check out the list methods.
The appendleft() function performs the push-like operation from the front of the double-ended queue. But for general lists, which are implemented as dynamic arrays, prepending is O(n) time.
# Define a collection of data from collections import deque sample_list = deque([6, 9, 12, 15]) print("Before Prepending:", sample_list) sample_list.appendleft(3) print("After Prepending:", sample_list) # Before Prepending: deque([6, 9, 12, 15]) # After Prepending: deque([3, 6, 9, 12, 15])
Pros
- The collections.deque.appendleft() method is lightning-fast for large datasets because the prepend time is O(1).
- Built for efficient insertions or deletions at both ends.
- It is an efficient approach for frequent prepends.
Cons
- It requires converting list ↔ deque (O(n) initial cost).
- It depends on a “collections” package.
Method 5: Using list unpacking
The * operator(works well in Python 3.5 and later) unpacks the elements of the list and prepends item 3 to the existing list.
sample_list = [6, 9, 12, 15] print("Before Prepending:", sample_list) sample_list = [3, *sample_list] print("After Prepending:", sample_list) # Before Prepending: [6, 9, 12, 15] # After Prepending: [3, 6, 9, 12, 15]
Pros
- The original list remains unchanged.
- It provides a cleaner syntax to combine lists or elements.
Cons
- It creates a new list (O(n) time/space). So, it is not memory and time-efficient.
- Not backward compatible with older versions of Python.
Conclusion
For large datasets and frequent prepending, you should use collections.deque.appendleft().
For in-place modification, use insert() or slice assignment.