Skip to content

Change List Items

Python lists are one of the most widely used data structures in Python programming. A list is an ordered collection of items that can contain different data types, such as numbers, strings, or other lists. In this chapter, we will explore the different ways to modify lists in Python.

Changing a list in Python

There are several ways to change a list in Python. We will cover the most commonly used methods in this section.

Append() method

The append() method adds an item to the end of a list. Use the following syntax:

Append an item to a list in Python

python
my_list.append(item)

Here, my_list is the name of the list, and item is the value to be added. Let's look at an example:

Append an item to a list in Python example

python
my_list = [1, 2, 3, 4]
my_list.append(5)
print(my_list)

Output:

console
[1, 2, 3, 4, 5]

Insert() method

The insert() method adds an item at a specific position in a list. Use the following syntax:

Insert an item to a specific position of a list in Python

python
my_list.insert(index, item)

Here, my_list is the name of the list, index is the position where you want to insert the item, and item is the value to be added. Let's look at an example:

Insert an item to a specific position of a list in Python example

python
my_list = [1, 2, 3, 4]
my_list.insert(2, 5)
print(my_list)

Output:

console
[1, 2, 5, 3, 4]

Extend() method

The extend() method adds the items of another list to the end of a list. Use the following syntax:

Add the items of another list to the end of a list in Python

python
my_list.extend(another_list)

Here, my_list is the name of the list, and another_list is the list whose items you want to add. Let's look at an example:

Add the items of another list to the end of a list in Python example

python
my_list = [1, 2, 3, 4]
another_list = [5, 6, 7]
my_list.extend(another_list)
print(my_list)

Output:

console
[1, 2, 3, 4, 5, 6, 7]

Slicing

Slicing is a technique to extract a part of a list, but it can also be used to replace a portion of a list. Use the following syntax:

Slicing in Python

python
my_list[start_index:end_index] = new_list

Here, my_list is the name of the list, start_index is the index of the first item to be changed, end_index is the index of the last item to be changed plus one, and new_list is the list of new items to be inserted. Let's look at an example:

Slice and concatenate lists in Python

python
my_list = [1, 2, 3, 4]
my_list[1:3] = [5, 6]
print(my_list)

Output:

console
[1, 5, 6, 4]

List Comprehension

List comprehension is a concise way to create a new list based on an existing list. Note that this approach creates a new list and reassigns it to the variable, rather than mutating the original list in place. To update a list using list comprehension, use the following syntax:

List comprehension in Python

python
my_list = [expression for item in my_list if condition]

Here, expression is the operation to be performed on each item in the list, item is the variable representing each item in the list, my_list is the name of the list, and condition is the filter condition to include items in the new list. Let's look at an example:

Use list comprehension to filter a list in Python

python
my_list = [1, 2, 3, 4]
my_list = [item * 2 for item in my_list if item % 2 == 0]
print(my_list)

Output:

console
[4, 8]

Conclusion

In this article, we discussed the different ways to change Python lists. We covered the append(), insert(), and extend() methods, slicing, and list comprehension. These methods provide flexible ways to modify lists in Python, based on your specific requirements. We hope this article was helpful and provided valuable insights into Python lists. If you have any questions or suggestions, feel free to leave a comment below.

Practice

In Python, how can we change items in a list?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.