Change multiple value in list python

In this article, I will explain you how to replace some values dynamically in a list in python based on conditions .

In this article, we will discuss different scenarios with step-by-step examples to replace values in a list in python.

Example #1 How to replace single value in list in python

Lets assume we have dataset as below

a = [6,10,15,20,25]

Here we will change the first value of the list with 5 using below python code.

#create the list of 5 data items a = [6,10,15,20,25] #view the list before replacement print['The given list is:',a] #replace first item value in list a[0] = 0 #view updated list print["The Updated list is: ",a]

In the above code, we changed the first value in the list using index position.

As the index value of the first element in the list is 0. So using a[0] = 5 we replace the first value with 5.

The output of the above python code is shown below.

//Output The given list is: [6, 10, 15, 20, 25] The modified list is: [5, 10, 15, 20, 25]

Example #2 How to replace the specific value in list in python

In this example, we will see how to replace the specific value according to given conditions.

Lets assume we have dataset given below:-

a = [6,10,15,20,8,25]

In this example, we will replace the elements with value 0 which are divisible by 5 using the below python code.

#create the list of 5 data items a = [6,10,15,20,8,25] #view the list before replacement print['The given list is:',a] #replace only those values with 0 which are divisible by 5 a = [0 if [x%5]==0 else x for x in a] #view updated list print["The Updated list is: ",a]

In the above code, we are applying the modulo to check whether elements are divisible by 5 or not. Based on the results, it will replace a value in a list python program.

By using a = [0 if [x%5]==0 else x for x in a] ,we replacing the elements with 0 value which are divisible by 5.

The Output of the above python code is shown below.

#Output The given list is: [6, 10, 15, 20, 8, 25] The Updated list is: [6, 0, 0, 0, 8, 0]

Example #3 How to replace multiple values in list in python

In this example, we will see how to replace the multiple value in list using below python code.

#create the list of 5 data items a = [1,2,3,4,5] #view the list before replacement print['The given list is:',a] #replace the first 3 values a[0:3] = [-1,-2,-3] #view updated list print["The Updated list is: ",a]

In the above code ,using a[0:3] = [-1,-2,-3] , it will replace first 3 value in a list.

The output of the above code is shown below.

Note:- While assigning values it will not consider the last index thats why we are using index 0 to 3,so that the elements at position 0,1,2 i.e. first three values got replaced properly.

#Output The given list is: [1, 2, 3, 4, 5] The Updated list is: [-1, -2, -3, 4, 5]

Example #4 How to find and replace the specific value in the list in python

In this example, we will first find the value in list and then replace that with the new value using below python code.

#create the list of 5 data items a = [0,2,3,4,5] #view the list before replacement print['The given list is:',a] #replace only 0 value with 1 a = [1 if [x==0] else x for x in a] #view updated list print["The Updated list is: ",a]

In the above code, Firstly using a = [1 if [x==0] else x for x in a] we are checking whether element value is 0 or not.

Based on the results, it will replace the elements with 1 which are equal to 0.

The Output of the above code is shown below.

#Output The given list is: [0, 2, 3, 4, 5] The Updated list is: [1, 2, 3, 4, 5]

Conclusion

I hope, you may findhow to replace values in a list in pythontutorial with step by step illustration of examples educational and helpful.

Video liên quan

Chủ Đề