How to Add an Element to a Python List at an Index?

To add an element to a given Python list, you can use either of the three following methods:

  1. Use the list insert method list.insert(index, element).
  2. Use slice assignment lst[index:index] = [element] to overwrite the empty slice with a list of one element.
  3. Use list concatenation with slicing lst[:2] + ['Alice'] + lst[2:] to create a new list object.

In the following, you’ll learn about all three methods in greater detail. But before that, feel free to test those yourself in our interactive Python shell (just click “Run” to see the output):

Method 1: insert(index, element)

The list.insert(i, element) method adds an element element to an existing list at position i. All elements j>i will be moved by one index position to the right.

Here’s an example with comments:

# Create the list
lst = [2, 4, 6, 8]

# Insert string at index 2
lst.insert(2, 'Alice')

# Print modified list object
print(lst)
# [2, 4, 'Alice', 6, 8]
Properties of insert()
Operates on existing list object
Simple
Fast

Check out the objects in memory while executing this code snippet (in comparison to the other methods discussed in this article):

Click “Next” to move on in the code and observe the memory objects creation.

Related article: Python List insert() Method

Method 2: Slice Assignment

Slice assignment is a little-used, beautiful Python feature to replace a slice with another sequence.

Simply select the slice you want to replace on the left and the values to replace it on the right side of the equation.

For example, the slice assignment list[2:4] = [42, 42] replaces the list elements with index 2 and 3 with the value 42.

Here’s another example that shows you how to insert the string 'Alice' into a list with four integers.

Let’s have a look at the code:

# Create the list
lst = [2, 4, 6, 8]

# Insert string at index 2
lst[2:2] = ['Alice']

# Print modified list object
print(lst)
# [2, 4, 'Alice', 6, 8]
Properties of Slice Assignment
Operates on existing list object
Slightly more complex
Fast

Related article: Python Slice Assignment

Method 3: List Concatenation

If you use the + operator on two integers, you’ll get the sum of those integers. But if you use the + operator on two lists, you’ll get a new list that is the concatenation of those lists.

Here’s the same example, you’ve already seen in the previous sections:

# Create the list
lst = [2, 4, 6, 8]

# Insert string at index 2
lst = lst[:2] + ['Alice'] + lst[2:]

# Print modified list object
print(lst)
# [2, 4, 'Alice', 6, 8]
Properties of List Concatenation
Creates a new list object
Slightly more complex
Slower

Related article: How to Concatenate Lists in Python? [Interactive Guide]

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!