In this Python program, we will show the steps involved in changing the list items using index position, slicing, loops, lambda, map, and list comprehension. As we already know, a list is mutable, so after creating the list, we can change the items at any position.
Python Program to Change List Items using Index
In the example below, we have created a simple integer list with six items. Next, we used the index position to change or replace the 4th item to 100. Remember that index position starts from 0 to n-1, so a[3] means the 4th item.
a = [5, 10, 15, 20, 25, 30]
print(a)
a[3] = 100
print(a)
[5, 10, 15, 20, 25, 30]
[5, 10, 15, 100, 25, 30]
[5, 10, 15, 20, 25, 30]
[5, 10, 15, 100, 25, 30]
Using Slice
The approach mentioned above will change one list item at a time. However, you can use the list slicing technique to change multiple items in one go. For example, the first statement a[2:6] in the below Python program will change the list items of the index position from 2 to 6 (2, 3, 4, and 5). The next statement, a[2: 7], replaces 5 items (2, 3, 4, 5, and 6) with four items.
a = [5, 10, 15, 20, 25, 30, 35, 40]
print(a)
a[2:6] = [100, 200, 300, 400]
print(a)
a[2:7] = [100, 200, 300, 400]
print(a)
The above two methods are helpful to change the last items in an order. However, you can use the approaches below to change the list items based on the condition or expression.
Python Program to Change List Items using the for loop
The below program uses the for loop to start the list items from 0 to the length of a list. Next, the if condition checks whether the index position is even or not. If true, add 100 to the existing value.
a = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
print(a)
for i in range(len(a)):
if i % 2 == 0:
a[i] += 100
print(a)
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
[110, 20, 130, 40, 150, 60, 170, 80, 190, 100]
Using the for loop and enumerator
Instead of using the len() function, we use the for loop enumerator to extract the index and the value. Here, i is the index position, and n is the actual value at that position. If the (if n % 3 == 0) value is completely divisible by three, add 200 to the existing value.
a = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
print(a)
for i, n in enumerate(a):
if n % 3 == 0:
a[i] += 200
print(a)
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
[10, 20, 230, 40, 50, 260, 70, 80, 290, 100]
Using list comprehension
It is the best and easiest option to update or change the list of items based on the condition. The below program list comprehension will multiply the list element by 3 if the value is divisible by 4.
a = [10, 20, 30, 40, 50, 60]
print(a)
a = [n * 3 if n % 4 == 0 else n for n in a]
print(a)
[10, 20, 30, 40, 50, 60]
[10, 60, 30, 120, 50, 180]
Using map()
Apart from the above approaches, you can use the map() function to change each and every list. Item. The below Python program will change or divide each list item by 2 using map and lambda.
a = [10, 20, 30, 40, 50, 60]
print(a)
a = list(map(lambda n: n // 2, a))
print(a)
[10, 20, 30, 40, 50, 60]
[5, 10, 15, 20, 25, 30]