• 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 / Using Python to Insert Item Into List

Using Python to Insert Item Into List

August 26, 2022 Leave a Comment

To insert an item into a list in Python, you can use the list insert() function. Pass the index and an object to insert to insert() to insert an object at a certain position.

lst = [0, 2, 4]

lst.insert(2, 3)

print(lst)

#Output:
[0, 2, 3, 4]

When working with collections of data, the ability to make changes and modify these collections easily is valuable.

One such case is if you want to insert an item into a list in your Python code.

To insert an item into a list in Python, you can use the list insert() function.

insert() takes in two parameters. The first parameter is the index of where you want to insert an object. The second parameter is the object you want to insert.

Below is a simple example of how you can insert an item into a list using Python.

lst = [0, 2, 4]

lst.insert(2, 3)

print(lst)

#Output:
[0, 2, 3, 4]

Inserting Multiple Items into List Using Python

If you want to insert multiple items into a list using Python, you can use the insert() function multiple times or you could use list slicing.

For example, let’s say you have a second list and want to insert this second list into the first list at the position 3.

list_1 = [0, 1, 2, 3, 6, 7]
list_2 = [4, 5]

You could use the insert() function in a loop and insert each item of the second list into the first list as shown below.

list_1 = [0, 1, 2, 3, 6, 7]
list_2 = [4, 5]

idx = 4

for item in list_2:
    list_1.insert(idx, item)
    idx = idx + 1

print(list_1)

#Output:
[0, 1, 2, 3, 4, 5, 6, 7]

One other way you could do this is with list slicing.

You can create two lists by slicing the list in two parts depending on where you want to make the insertion and then concatenate all of the lists together with +.

Below shows you how you can use list slicing to insert a list into a list using Python.

list_1 = [0, 1, 2, 3, 6, 7]
list_2 = [4, 5]

list_1 = list_1[:4] + list_2 + list_1[4:]

print(list_1)

#Output:
[0, 1, 2, 3, 4, 5, 6, 7]

Hopefully this article has been useful for you to learn how to insert into a list in Python.

Other Articles You'll Also Like:

  • 1.  math.degrees() Python – How to Convert Radians to Degrees in Python
  • 2.  Format Numbers as Currency with Python
  • 3.  Python Random Boolean – How to Generate Random Boolean Values
  • 4.  Check if Variable is Datetime in Python
  • 5.  pandas median – Find Median of Series or Columns in DataFrame
  • 6.  How to Iterate over Everything in Word Document using python-docx
  • 7.  pandas nlargest – Find Largest Values in Series or Dataframe
  • 8.  Using Python to Remove Last Character from String
  • 9.  Get Elapsed Time in Seconds in Python
  • 10.  Get Days of timedelta Object 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