In Python, there are always multiple ways to accomplish the same thing—but with subtle differences in the side effects. A great coder will always choose the most effective way for the problem at hand.
This tutorial shows you six different ways to add elements to a list in Python. In a nutshell, the different ways to add one or more elements to a list are:
- append(): add a single element to an existing list.
- extend(): add multiple elements to an existing list.
- insert(): add an element at an arbitrary position in an existing list.
- Slice assignment: replace a slice of an existing list.
- List concatenation with +: add one or more elements to a new list.
- Asterisk operator *: unpack multiple iterables into a new list.
Try It Yourself: Before we dive into each of those methods, let’s try them yourself in our interactive Python shell!
Exercise: Use each method to add yet another integer element 42
to each list. Which method is the best one to add multiple elements?
Next, you’ll learn about each method in a video tutorial and short example code snippet. I’ve written in-depth articles for each method so feel free to follow the references given in each method.
Method 1: append()
The list.append(x)
method—as the name suggests—appends element x
to the end of the list
. You can call this method on each list object in Python. Here’s the syntax:
list.append(element)
Argument | Description |
---|---|
element | The object you want to append to the list. |
# Method 1: append() friends = ['Alice', 'Bob', 'Ann'] friends.append('Liz') print(friends) # ['Alice', 'Bob', 'Ann', 'Liz']
Read More: Python List append() Method
Method 2: extend()
The list.extend(iter)
method adds all elements in the argument iterable iter
to an existing list
. You can call this method on each list object in Python. Here’s the syntax:
list.extend(iterable)
Argument | Description |
---|---|
iterable | All the elements of the iterable will be added to the end of the list—in the order of their occurrence. |
# Method 2: extend() friends = ['Alice', 'Bob', 'Ann'] friends.extend(['Liz', 'Carl']) print(friends) # ['Alice', 'Bob', 'Ann', 'Liz', 'Carl']
Read More: Python List extend() Method
Method 3: insert()
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. You can call this method on each list object in Python. Here’s the syntax:
list.insert(index, element)
Argument | Description |
---|---|
index | Integer value representing the position before you want to insert an element |
element | Object you want to insert into the list. |
# Method 3: insert() friends = ['Alice', 'Bob', 'Ann'] friends.insert(3, 'Liz') print(friends) # ['Alice', 'Bob', 'Ann', 'Liz']
Read More: Python List insert() Method
Method 4: 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
.
# Method 4: slice assignment friends = ['Alice', 'Bob', 'Ann'] friends[3:3] = ['Liz'] print(friends) # ['Alice', 'Bob', 'Ann', 'Liz']
Read More: Python Slice Assignment
Method 5: List Concatenation with +
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.
# Method 5: list concatenation friends = ['Alice', 'Bob', 'Ann'] friends = friends + ['Liz'] print(friends) # ['Alice', 'Bob', 'Ann', 'Liz']
Read More: Python List Concatenation
Method 6: List Concatenation with Unpacking *
There are many applications of the asterisk operator. But one nice trick is to use it as an unpacking operator that “unpacks” the contents of a container data structure such as a list or a dictionary into another one.
# Method 6: unpacking friends = ['Alice', 'Bob', 'Ann'] friends = [*friends, 'Liz'] print(friends) # ['Alice', 'Bob', 'Ann', 'Liz']
A great advantage is that you can quickly unpack all elements of two or more list into a new list.
Read More: Python Unpacking with Asterisk
Python List Methods Cheat Sheet
Here’s your free PDF cheat sheet showing you all Python list methods on one simple page. Click the image to download the high-resolution PDF file, print it, and post it to your office wall:
Discussion
Let’s summarize the strengths and weaknesses of the different methods:
- Use the
append()
method to add a single element to an existing list without creating a new list. - Use the
extend()
method to add multiple elements to an existing list without creating a new list. - Use the
insert()
method to add an element at an arbitrary position in the list—without creating a new list. - Use slice assignment to replace a slice of an existing list—without creating a new list.
- Use list concatenation with
+
to add one or more elements to a list—if you want to create a new list. - Use the asterisk operator
*
to unpack multiple iterables into a new list—if you want to create a new list.
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.