• 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
  • VBA
  • 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.  Using Python to Capitalize Every Other Letter of String
  • 2.  Symmetric Difference of Two Sets in Python
  • 3.  pandas sum – Get Sum of Series or DataFrame Columns
  • 4.  Python Get Yesterday’s Date
  • 5.  How to Iterate over Everything in Word Document using python-docx
  • 6.  Python acos – Find Arccosine and Inverse Cosine of Number
  • 7.  Pascal’s Triangle in Python
  • 8.  Python Delete Variable – How to Delete Variables with del Keyword
  • 9.  Initialize Multiple Variables in Python
  • 10.  How to Check if String Contains Uppercase Letters 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy