Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Modifying a List | List
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

bookModifying a List

In Python, lists are mutable, which means that after a list is created, its contents can be modified, added, or removed. Because of this mutability, lists have much flexibility for working with lists and are, therefore, a very powerful tool for managing dynamic data.

What Does "Mutable" Mean?

Mutable means that the data structure can be updated without creating a new object. For example, you could change the value of an existing item, replace several elements, or even remove and insert new ones directly into the same list.

Imagine you have a list of cities you plan to visit, but plans change. You can update or replace items in the list easily:

123456789
cities = ["Paris", "Tokyo", "New York", "Berlin", "Sydney"] # Replacing the fourth city cities[2] = "Rome" print(cities) # Output: ['Paris', 'Tokyo', 'Rome', 'Berlin', 'Sydney'] # Replacing the last two cities with new ones cities[-2:] = ["Dubai", "Cape Town"] print(cities) # Output: ['Paris', 'Tokyo', 'Rome', 'Dubai', 'Cape Town']
copy

In this example:

  1. We replaced the city "New York" at index 3 with "Rome";
  2. Using negative indexing, we replaced the last two cities ("Rome" and "Sydney") with "Dubai" and "Cape Town".

You can also make multiple changes in one step:

123456
cities = ["Paris", "Tokyo", "New York", "Berlin", "Sydney"] # Replacing multiple cities in the middle cities[1:3] = ["Seoul", "Bangkok", "Mumbai"] print(cities) # Output: ['Paris', 'Seoul', 'Bangkok', 'Mumbai', 'Berlin', 'Sydney']
copy

Here, we replaced "Tokyo" and "New York" with three cities: "Seoul", "Bangkok", and "Mumbai". This demonstrates how flexible list mutability can be for managing your data.

Task
test

Swipe to show code editor

Update the estimated cost of your first city to include a discount. Apply a 20% reduction to the cost.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
toggle bottom row

bookModifying a List

In Python, lists are mutable, which means that after a list is created, its contents can be modified, added, or removed. Because of this mutability, lists have much flexibility for working with lists and are, therefore, a very powerful tool for managing dynamic data.

What Does "Mutable" Mean?

Mutable means that the data structure can be updated without creating a new object. For example, you could change the value of an existing item, replace several elements, or even remove and insert new ones directly into the same list.

Imagine you have a list of cities you plan to visit, but plans change. You can update or replace items in the list easily:

123456789
cities = ["Paris", "Tokyo", "New York", "Berlin", "Sydney"] # Replacing the fourth city cities[2] = "Rome" print(cities) # Output: ['Paris', 'Tokyo', 'Rome', 'Berlin', 'Sydney'] # Replacing the last two cities with new ones cities[-2:] = ["Dubai", "Cape Town"] print(cities) # Output: ['Paris', 'Tokyo', 'Rome', 'Dubai', 'Cape Town']
copy

In this example:

  1. We replaced the city "New York" at index 3 with "Rome";
  2. Using negative indexing, we replaced the last two cities ("Rome" and "Sydney") with "Dubai" and "Cape Town".

You can also make multiple changes in one step:

123456
cities = ["Paris", "Tokyo", "New York", "Berlin", "Sydney"] # Replacing multiple cities in the middle cities[1:3] = ["Seoul", "Bangkok", "Mumbai"] print(cities) # Output: ['Paris', 'Seoul', 'Bangkok', 'Mumbai', 'Berlin', 'Sydney']
copy

Here, we replaced "Tokyo" and "New York" with three cities: "Seoul", "Bangkok", and "Mumbai". This demonstrates how flexible list mutability can be for managing your data.

Task
test

Swipe to show code editor

Update the estimated cost of your first city to include a discount. Apply a 20% reduction to the cost.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 5
toggle bottom row

bookModifying a List

In Python, lists are mutable, which means that after a list is created, its contents can be modified, added, or removed. Because of this mutability, lists have much flexibility for working with lists and are, therefore, a very powerful tool for managing dynamic data.

What Does "Mutable" Mean?

Mutable means that the data structure can be updated without creating a new object. For example, you could change the value of an existing item, replace several elements, or even remove and insert new ones directly into the same list.

Imagine you have a list of cities you plan to visit, but plans change. You can update or replace items in the list easily:

123456789
cities = ["Paris", "Tokyo", "New York", "Berlin", "Sydney"] # Replacing the fourth city cities[2] = "Rome" print(cities) # Output: ['Paris', 'Tokyo', 'Rome', 'Berlin', 'Sydney'] # Replacing the last two cities with new ones cities[-2:] = ["Dubai", "Cape Town"] print(cities) # Output: ['Paris', 'Tokyo', 'Rome', 'Dubai', 'Cape Town']
copy

In this example:

  1. We replaced the city "New York" at index 3 with "Rome";
  2. Using negative indexing, we replaced the last two cities ("Rome" and "Sydney") with "Dubai" and "Cape Town".

You can also make multiple changes in one step:

123456
cities = ["Paris", "Tokyo", "New York", "Berlin", "Sydney"] # Replacing multiple cities in the middle cities[1:3] = ["Seoul", "Bangkok", "Mumbai"] print(cities) # Output: ['Paris', 'Seoul', 'Bangkok', 'Mumbai', 'Berlin', 'Sydney']
copy

Here, we replaced "Tokyo" and "New York" with three cities: "Seoul", "Bangkok", and "Mumbai". This demonstrates how flexible list mutability can be for managing your data.

Task
test

Swipe to show code editor

Update the estimated cost of your first city to include a discount. Apply a 20% reduction to the cost.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

In Python, lists are mutable, which means that after a list is created, its contents can be modified, added, or removed. Because of this mutability, lists have much flexibility for working with lists and are, therefore, a very powerful tool for managing dynamic data.

What Does "Mutable" Mean?

Mutable means that the data structure can be updated without creating a new object. For example, you could change the value of an existing item, replace several elements, or even remove and insert new ones directly into the same list.

Imagine you have a list of cities you plan to visit, but plans change. You can update or replace items in the list easily:

123456789
cities = ["Paris", "Tokyo", "New York", "Berlin", "Sydney"] # Replacing the fourth city cities[2] = "Rome" print(cities) # Output: ['Paris', 'Tokyo', 'Rome', 'Berlin', 'Sydney'] # Replacing the last two cities with new ones cities[-2:] = ["Dubai", "Cape Town"] print(cities) # Output: ['Paris', 'Tokyo', 'Rome', 'Dubai', 'Cape Town']
copy

In this example:

  1. We replaced the city "New York" at index 3 with "Rome";
  2. Using negative indexing, we replaced the last two cities ("Rome" and "Sydney") with "Dubai" and "Cape Town".

You can also make multiple changes in one step:

123456
cities = ["Paris", "Tokyo", "New York", "Berlin", "Sydney"] # Replacing multiple cities in the middle cities[1:3] = ["Seoul", "Bangkok", "Mumbai"] print(cities) # Output: ['Paris', 'Seoul', 'Bangkok', 'Mumbai', 'Berlin', 'Sydney']
copy

Here, we replaced "Tokyo" and "New York" with three cities: "Seoul", "Bangkok", and "Mumbai". This demonstrates how flexible list mutability can be for managing your data.

Task
test

Swipe to show code editor

Update the estimated cost of your first city to include a discount. Apply a 20% reduction to the cost.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 5
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt