Python
List Manipulation
Prepend
Programming
Coding Tips

How do I prepend to a short python list?

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

If the list is short, prepending in Python is mostly about choosing the clearest expression rather than chasing tiny performance gains. Python lists do not have a dedicated prepend() method, but the language gives you a few good options depending on whether you want to mutate the original list or create a new one.

The Normal In-Place Solution: insert(0, value)

The standard way to add one item to the front of an existing list is insert.

python
1items = [2, 3, 4]
2items.insert(0, 1)
3
4print(items)

Output:

python
[1, 2, 3, 4]

This mutates the list in place, which is usually exactly what you want when the list already belongs to the current piece of code.

Under the hood, Python shifts the existing elements to the right. That makes prepending less efficient than append(), but for small lists the difference rarely matters.

Create A New List With Concatenation

If you want to keep the original list unchanged, build a new list instead.

python
1items = [2, 3, 4]
2new_items = [1] + items
3
4print(items)
5print(new_items)

Output:

python
[2, 3, 4]
[1, 2, 3, 4]

This version is often clearer in functional-style code or when the prepend happens inside an expression.

Prepending Several Values

If you need to add multiple items to the front while preserving their order, slice assignment is convenient.

python
1items = [3, 4, 5]
2items[:0] = [1, 2]
3
4print(items)

Output:

python
[1, 2, 3, 4, 5]

This is more readable than calling insert(0, value) repeatedly, and it avoids accidentally reversing the prepended items.

You can also create a new list if mutation is not desired:

python
items = [3, 4, 5]
result = [1, 2] + items
print(result)

When deque Is The Better Data Structure

The question says the list is short, so a regular list is fine. Still, it is worth knowing when the requirement changes. If your algorithm prepends frequently, collections.deque is designed for efficient operations at both ends.

python
1from collections import deque
2
3items = deque([2, 3, 4])
4items.appendleft(1)
5
6print(items)
7print(list(items))

A deque is a better structural choice when left-end insertion is a core operation rather than an occasional convenience.

Pick The Method By Intent

A useful rule of thumb is:

  • use insert(0, value) when you want to mutate the current list
  • use [value] + items when you want a new list
  • use items[:0] = values when adding several leading values
  • use deque.appendleft() when front insertion is frequent enough to matter structurally

For a short list, readability should drive the choice more than raw complexity analysis.

Common Pitfalls

The most common mistake is over-optimizing a tiny list. If the list has only a few elements, insert(0, value) is almost always fine and much clearer than changing data structures prematurely.

Another issue is choosing concatenation when callers expect the original list to be modified. That silently creates a new list and leaves the old one untouched.

Repeated insert(0, value) calls can also reverse the order you intended if you are not careful. If you need to prepend several items, do it in one step with slice assignment or concatenate a list of the new values.

Finally, if prepending becomes a hot path, do not keep forcing a list to behave like a queue. Switch to deque and make the algorithm's intent explicit.

Summary

  • For a short list, insert(0, value) is the normal prepend operation.
  • Use concatenation when you want a new list instead of mutating the old one.
  • Use slice assignment to prepend multiple values cleanly.
  • Choose deque only when front insertions are frequent enough to justify a different structure.
  • For small lists, clarity matters more than micro-optimization.

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.