Updating and Changing Lists in Python
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:
123456789cities = ["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']
In this example, the city "New York" was replaced at index 2 with "Rome", and using negative indexing, the last two cities ("Berlin" and "Sydney") were replaced with "Dubai" and "Cape Town". Multiple changes can also be made in one step.
123456cities = ["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']
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.
Swipe to start coding
You have a travel_wishlist list.
- Update the estimated cost of all cities to include a discount.
- Apply a 20% reduction to the cost (the third element in the nested list).
- Use indexing to update the elements.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4
Updating and Changing Lists in Python
Swipe to show menu
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:
123456789cities = ["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']
In this example, the city "New York" was replaced at index 2 with "Rome", and using negative indexing, the last two cities ("Berlin" and "Sydney") were replaced with "Dubai" and "Cape Town". Multiple changes can also be made in one step.
123456cities = ["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']
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.
Swipe to start coding
You have a travel_wishlist list.
- Update the estimated cost of all cities to include a discount.
- Apply a 20% reduction to the cost (the third element in the nested list).
- Use indexing to update the elements.
Solution
Thanks for your feedback!
single