×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python - Change List Items

Last Updated : May 01, 2025

You can change items in a Python list by using indexing and other approaches. This will allow you to update one or more elements. In this chapter, we will learn about the different approaches to update (change) list items.

Changing a List Item Using Index

You can change an item in a list by accessing it using its index and assigning a new value to it. This allows you to modify specific items in the list based on their position.

Example

In this example, we are changing the city name at index 1 in the list of cities.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Berlin"]

# Changing the second item (index 1)
cities[1] = "Tokyo"

# Displaying the modified list
print(cities)

The output of the above code would be:

['New York', 'Tokyo', 'Paris', 'Berlin']

Changing Multiple List Items Using Slicing

You can also change multiple items in a list by using slicing. The slice syntax list[start:end] allows you to modify a specific range of elements.

Example

In this example, we are changing multiple city names in the list using slicing. The cities from index 1 to index 3 will be replaced.

# Creating a list of cities
cities = ["New York", "London", "Paris", "Berlin", "Tokyo"]

# Changing cities from index 1 to index 3
cities[1:4] = ["Sydney", "Toronto", "Dubai"]

# Displaying the modified list
print(cities)

The output of the above code would be:

['New York', 'Sydney', 'Toronto', 'Dubai', 'Tokyo']

Changing List Items Using a Loop

You can also loop through the list and change specific items based on a condition. This method allows you to make more dynamic changes to the list.

Example

In this example, we are changing the city names in the list that contain the letter "o".

# Creating a list of cities
cities = ["New York", "London", "Paris", "Berlin", "Tokyo"]

# Looping through the list and changing cities with the letter 'o'
for i in range(len(cities)):
    if "o" in cities[i]:
        cities[i] = cities[i] + " (Modified)"

# Displaying the modified list
print(cities)

The output of the above code would be:

['New York (Modified)', 'London (Modified)', 'Paris', 'Berlin', 'Tokyo (Modified)']

Exercise

Select the correct option to complete each statement about changing items in a Python list.

  1. Which index would you use to change the second item in a list my_list = [10, 20, 30]?
  2. How would you change the first item in the list my_list = [100, 200, 300] to 500?
  3. What will be the result of the following code for my_list = [10, 20, 30]:
    my_list[1] = 100?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.