Insert at first position of a list in Python
Codemia Knowledge Hub ·
Introduction
Python gives you several ways to add an item at the beginning of a sequence, but the right choice depends on whether you want to mutate the existing list, build a new one, or optimize for repeated front insertions.
A normal list can absolutely prepend items. The important detail is cost: every front insertion shifts the existing elements one position to the right.
Use insert(0, value) for Simple Cases
The most direct solution is list.insert. Passing 0 as the index places the new value at the first position and mutates the existing list in place.
This is the clearest option when you only need to prepend once in a while. It reads well and keeps the original list object alive, which matters if other code already holds a reference to it.
Use Slice Assignment When Adding Several Values
If you need to add more than one value at the front in one step, slice assignment can be convenient:
This still mutates the same list object, but it is often clearer than calling insert repeatedly.
Create a New List with Concatenation
If you want to keep the original list unchanged, build a new list instead.
This is useful when you want a more functional style or need to preserve the previous list for logging, undo behavior, or comparison. The tradeoff is that Python allocates a new list and copies references into it.
Prefer deque for Frequent Front Insertions
If your program prepends often, a list is not the best data structure. collections.deque is designed for efficient operations at both ends.
A deque is a better fit for queues, sliding windows, and stream-like workloads where front insertion is part of the normal design rather than a rare edge case.
Think About Cost, Not Just Syntax
All three approaches are valid, but they serve different goals:
- '
insert(0, value)is best for occasional in-place updates.' - '
items[:0] = new_valuesis useful when several items must be prepended at once.' - '
[value] + itemsis best when you want a separate list.' - '
deque.appendleftis best when prepending happens repeatedly.'
The important detail is performance. Appending to the end of a list is cheap, but inserting at the front requires moving every existing element one position to the right. That means the cost grows with list size. For a tiny list the difference is negligible, but for large collections in tight loops it becomes noticeable.
Example in a Real Function
Here is a small helper that prepends a header row while preserving the original list of rows:
This is easier to reason about than mutating the input list, especially in code that passes lists through several functions.
Common Pitfalls
- Assuming front insertion on a list is constant time. It is not.
- Using concatenation inside a large loop, which creates many temporary lists.
- Switching to
dequewithout checking whether the rest of the code expects normal list behavior. - Forgetting that
insertchanges the original list and affects every reference to it.
Summary
- Use
my_list.insert(0, value)for the simplest in-place prepend. - Use
[value] + my_listwhen you want a new list and need to preserve the old one. - Use
collections.dequewithappendleftfor workloads that prepend frequently. - Python lists are optimized for end-appends, not repeated front insertions.
- Choose the data structure based on the operation you perform most often.
Related reading
- insert, delete, max in O1
- Insert element into numpy array and get all rolled permutations
- Inserting an equal value element
- Insertion sort better than Bubble sort?
- Insert or delete a step in scikit-learn Pipeline
- Insert result of sklearn CountVectorizer in a pandas dataframe
- Int to byte array
- Internal Implementation of STLMAP in C
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.