• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Prepend to List with insert() Function

Python Prepend to List with insert() Function

February 17, 2022 Leave a Comment

In Python, the easiest way to prepend an item to a list is with the Python list insert() function.

list = [1,2,9,0,1,3]

list.insert(0,2)

#Output:
[2,1,2,9,0,1,3]

You can also use the deque appendleft function to prepend an item to a list.

from collections import deque

list = [1,2,9,0,1,3]

dequelist = deque(list)

dequelist.appendleft(2)

print(list(dequelist))

#Output:
[2,1,2,9,0,1,3]

In Python, lists are one of the most used data structures and allow us to work with collections of data easily. When working with lists, it is useful to be able to add or remove items from the list in an easy way.

With Python, we can easily prepend items to lists. There are multiple ways we can add items to the beginning of a list.

In Python, the easiest way to prepend an item to a list is with the Python list insert() function.

The insert() function takes in two arguments. The first argument is the position to insert an item, and the second argument is the item to insert.

To prepend to a list, we will insert at position ‘0’.

Below is an example of how to prepend an item to a list in Python.

list = [1,2,9,0,1,3]

list.insert(0,2)

#Output:
[2,1,2,9,0,1,3]

Using deque in Python to Remove the First Item from a List

Another way that you can prepend an item to a list is with the deque data structure from the Python collections module.

Deque, or doubly ended queue, is most useful if you need to quickly append or pop items from the beginning or end of your data. If you have a large collection of items, you deque can be faster than the similar list operations.

To add an item at the start of a list using deque, we convert the list to deque, use the appendleft() function, and then convert the result back to a list.

from collections import deque

list = [1,2,9,0,1,3]

dequelist = deque(list)

dequelist.appendleft(2)

print(list(dequelist))

#Output:
[2,1,2,9,0,1,3]

Using reverse() and append() to Add an Element at Beginning of a List in Python

One last method I’d like to share with you in this article is how to add an item at the beginning of a list using reverse() and append().

I wouldn’t recommend this as it’s not as efficient as the insert() method.

To use this way of adding an element at the the beginning of a list, you first reverse the list, use the append() function, and then reverse the list again.

Below is how to prepend an element to a list using reverse() and append().

list = [1,2,9,0,1,3]

list.reverse()
list.append(2)
list.reverse()

#Output:
[2,1,2,9,0,1,3]

Hopefully this article has been useful for you to learn how to prepend items to a list using Python.

Other Articles You'll Also Like:

  • 1.  Creating a List of Zeros in Python
  • 2.  How to Combine Dictionaries in Python
  • 3.  Using Python to Generate Random String of Specific Length
  • 4.  Get Days in Month Using Python
  • 5.  How to Split a String in Half Using Python
  • 6.  Using Python to Read File Word by Word
  • 7.  Python cos – Find Cosine of Number in Radians Using math.cos()
  • 8.  Create Empty Tuple in Python
  • 9.  How to Remove All Spaces from String in Python
  • 10.  Using Matplotlib and Seaborn to Create Pie Chart in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy