Google News
logo
Python Program to Change the value of a List Item
You can change the value of a list item in Python using the index of the item. Here's an example :
Program :
my_list = ['apple', 'banana', 'cherry']
print("Original List:", my_list)

# change the value of the second item
my_list[1] = 'orange'

print("Modified List:", my_list)
Output :
Original List: ['apple', 'banana', 'cherry']
Modified List: ['apple', 'orange', 'cherry']
In the above example, we first create a list `my_list` with three items. We then print the original list using `print("Original List:", my_list)`.

Next, we use the index `1` to access the second item in the list, which is `'banana'`. We then assign a new value `'orange'` to this item using the assignment operator `=`. The updated list is printed using `print("Modified List:", my_list)`.