Python
lists
list-manipulation
programming
tutorial

Insert at first position of a list in Python

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

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.

python
1numbers = [20, 30, 40]
2numbers.insert(0, 10)
3
4print(numbers)
text
[10, 20, 30, 40]

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:

python
1numbers = [30, 40]
2numbers[:0] = [10, 20]
3
4print(numbers)
text
[10, 20, 30, 40]

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.

python
1original = [20, 30, 40]
2updated = [10] + original
3
4print(original)
5print(updated)
text
[20, 30, 40]
[10, 20, 30, 40]

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.

python
1from collections import deque
2
3queue = deque([20, 30, 40])
4queue.appendleft(10)
5queue.appendleft(5)
6
7print(queue)
8print(list(queue))
text
deque([5, 10, 20, 30, 40])
[5, 10, 20, 30, 40]

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_values is useful when several items must be prepended at once.'
  • '[value] + items is best when you want a separate list.'
  • 'deque.appendleft is 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:

python
1def with_header(rows: list[str], header: str) -> list[str]:
2    return [header] + rows
3
4
5rows = ["Alice", "Bob", "Charlie"]
6result = with_header(rows, "Name")
7
8print(rows)
9print(result)
text
['Alice', 'Bob', 'Charlie']
['Name', 'Alice', 'Bob', 'Charlie']

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 deque without checking whether the rest of the code expects normal list behavior.
  • Forgetting that insert changes 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_list when you want a new list and need to preserve the old one.
  • Use collections.deque with appendleft for 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
Course
Intermediate
27 lessons
15 hours
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 course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.