Sling Academy
Home/Python/Using the list.insert() method in Python

Using the list.insert() method in Python

Last updated: June 12, 2023

Overview

In Python, the list.insert() method inserts an element at a given position in a list. Below is its syntax:

list.insert(i, x)

Parameters:

  • i: The index of the element before which to insert, and can be any integer. If it is negative, it counts from the end of the list. If it is larger than the length of the list, it inserts the element at the end of the list
  • x: The element to insert. It can be any object, such as a string, a number, another list, etc. If it is an iterable, such as a list or a tuple, it will insert the whole iterable as a single element (you will understand this better in the examples to come).

The list.insert() method does not return anything. It modifies the original list in place.

The performance of the list.insert() method depends on the size of the list and the position where you insert the element. Its time complexity is O(n). That means it takes O(n) time in the worst case, where n is the length of the list, because it has to shift all the elements after the insertion point to the right.

Now, it’s time for the interesting part: code and examples of using the list.insert() method in practice.

Examples

Inserting a single element at a specific position in a list

The code:

# a list of video games
games = [
    "Minecraft",
    "Fortnite",
    "CS:GO"
]

# Insert a game at index 2
games.insert(2, "League of Legends")
print(games)

Output:

['Minecraft', 'Fortnite', 'League of Legends', 'CS:GO']

Inserting an iterable as a single element in a list

This example demonstrates inserting a tuple into another list in Python:

my_list = [
    "Sling Academy",
    "Python",
    "Programming"
]

my_list.insert(1, (2023, 2024, 2025))
print(my_list)

Output:

['Sling Academy', (2023, 2024, 2025), 'Python', 'Programming']

Inserting an element at the end or the beginning of a list

The code:

my_list = ["one", "two", "three"]

# Insert "zero" to the beginning of the list
my_list.insert(0, "zero")

# Insert "four" to the end of the list
my_list.insert(len(my_list), "four")

print(my_list)

Output:

['zero', 'one', 'two', 'three', 'four']

Next Article: Sorting Lists in Python (4 Examples)

Previous Article: Python: How to Calculate the Average of a Numeric List

Series: Python List Tutorials (Basic and Advanced)

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots