Change List items in Python
Change List Items in Python
In Python, lists are mutable, meaning you can change, add, and remove items after the list has been created. This lesson will cover how to change individual list items, modify ranges of items, and insert new items into a list.
1. Change Item Value
To change the value of a specific item in a list, you can refer to its index number. Remember that Python uses zero-based indexing, so the first item is at index 0.
Example:
Change the second item in the list:
1 2 3thislist = ["apple", "banana", "cherry"] thislist[1] = "blackcurrant" # Changing "banana" to "blackcurrant" print(thislist) # Output: ['apple', 'blackcurrant', 'cherry']
2. Change a Range of Item Values
You can also change the values of items within a specific range. To do this, define a new list with the desired values and specify the range of index numbers you want to change.
Example:
Change the values "banana" and "cherry" to "blackcurrant" and "watermelon":
1 2 3thislist = ["apple", "banana", "cherry", "orange", "kiwi", "mango"] thislist[1:3] = ["blackcurrant", "watermelon"] print(thislist) # Output: ['apple', 'blackcurrant', 'watermelon', 'orange', 'kiwi', 'mango']
Note on Inserting More Items
If you insert more items than you replace, the new items will be inserted at the specified index, and the remaining items will shift accordingly.
Example:
Change the second value by replacing it with two new values:
1 2 3thislist = ["apple", "banana", "cherry"] thislist[1:2] = ["blackcurrant", "watermelon"] print(thislist) # Output: ['apple', 'blackcurrant', 'watermelon', 'cherry']
Note on Inserting Fewer Items
If you insert fewer items than you replace, the new items will still be inserted at the specified index, and the remaining items will adjust accordingly.
Example:
Change the second and third values by replacing them with one value:
1 2 3thislist = ["apple", "banana", "cherry"] thislist[1:3] = ["watermelon"] print(thislist) # Output: ['apple', 'watermelon']
3. Insert Items
To add a new item to a list without replacing any existing items, you can use the insert() method. This method allows you to specify the index at which you want to insert the new item.
Example:
Insert "watermelon" as the third item:
1 2 3thislist = ["apple", "banana", "cherry"] thislist.insert(2, "watermelon") # Inserting "watermelon" at index 2 print(thislist) # Output: ['apple', 'banana', 'watermelon', 'cherry']
Frequently Asked Questions
In Python, you can replace an item in a list by accessing its index and assigning a new value to it. For example, my_list[2] = "new_value" replaces the item at index 2 with "new_value".
To change certain items in a list, you can use slicing to access a subset of items and then assign new values. For example, my_list[1:3] = ["new_item1", "new_item2"] changes items at index 1 and 2.
You can update a list item by assigning a new value to the specific index of the list. For example, my_list[3] = "updated_value" updates the item at index 3.
To change a value in a list of lists, you access the list and then the specific sublist, followed by the index of the item you want to update. For example, my_list[0][1] = "new_value" changes the second item in the first sublist.
Still have questions?Contact our support team