Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Prepend to List in Python [5 Ways]

  • 31 Jan, 2025
  • Com 0
Prepending elements to a list in Python

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:

  1. Using insert()
  2. Using list slicing
  3. Using concatenation (+ operator)
  4. Using collections.deque.appendleft()
  5. 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.

Using insert() to prepend an element to a 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

  1. It works with all Python versions. (No version restrictions).
  2. It has a simple syntax which is easy for beginners.

Cons

  1. It shifts existing elements to the right side, which takes time. That’s why time complexity is O(n).
  2. It is inefficient for large lists.
  3. 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.

Using list slicing to prepend an item to a list in Python

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

  1. You can easily prepend multiple elements.

Cons

  1. It has a less intuitive syntax.
  2. 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.

Using concatenation (+ operator) to prepend an element to a python list

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

  1. This approach clearly expressed its intent by putting a list of elements to the front.
  2. 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.
  3. It is a simple method for single or multiple elements.

Cons

  1. 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

  1. The collections.deque.appendleft() method is lightning-fast for large datasets because the prepend time is O(1).
  2. Built for efficient insertions or deletions at both ends.
  3. It is an efficient approach for frequent prepends.

Cons

  1. It requires converting list ↔ deque (O(n) initial cost).
  2. 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.

Using list unpacking to prepend an element

 

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

  1. The original list remains unchanged.
  2. It provides a cleaner syntax to combine lists or elements.

Cons

  1. It creates a new list (O(n) time/space). So, it is not memory and time-efficient.
  2. 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.

Post Views: 15
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Converting Numpy Array to Pandas DataFrame
How to Print List without Brackets in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend