Replace Item in List in Python

Replace Item in List in Python

Replacing an item in a list in python can be done in several ways. Two of the most common ways are using list indexing or a for loop to replace an item. A list comprehension can be used to create a new list based on an existing list and make a change. Replacing items in the list can be used at several places. For example, if you are designing a menu for a restaurant using python and notice that an item has been misspelled, you can run through it and change that item in the list using the methods discussed below.

Now it’s your turn to decide what your question demands to implement, whether list indexing or for loop or list comprehension. You can decide this by asking yourself some questions. Do you want to replace one or two items on a list? Then you can use list indexing and can replace the item directly. Do you want to traverse the whole list and then replace items in the list based on the following conditions? Then you can use a for loop to implement the same, and your problem will be solved.

Let’s take another example, you are making a list of guests at your hotel, and suddenly you saw that you had misspelled the guest’s name. To fix this problem, you will need to change that particular name spelling, not the whole list.

Replace Item in List in Python

As we have discussed that there are mainly three methods to replace items in the list. So, now we will be summarizing each of them.

  • List Indexing:  We can replace an item by its index in the list.
  • For loop:  The loop will traverse through the list, and based on the condition, we can use indexing and replace the item in the list.
  • List Comprehension: It creates a new list from an existing list, and based on your condition, you can determine the values in the list.

Now, we will have a walk-through of each method of replacing an item in a list in python. We have discussed an example so that you can relate to it and so that you can implement the same in your code.

Replace Item in List: Using List Indexing

This way to replace an item in the list is the easiest to implement compared to the others that can be used in Python. In this method of replacing an item in the list, we can choose the index of an element or the range of indexes of elements in the list. With the help of the assignment operator [=], we can replace the item at the given position in a list. Let’s understand this with the help of an example.

Suppose there is a list of marks of students according to their performance in the test. Now while cross-checking the marks, it was found that 3rd student’s marks are not correctly uploaded. We need to correct that, and for that, we will use the List Indexing method of replacing an item in a list in python.

Now, let’s get started by creating a list that contains the marks of the students:

studentMarks =[90, 74, 58, 84, 65, 79, 60, 87, 69, 76]

Now, we will change the marks of the 3rd student by selecting the marks with the help of indexing and assigning the new one to the same with the help of the assignment operator. [Note: Indexing starts with the first index as 0.]

studentMarks[2] = 85

print(studentMarks)

In this set of lines above, we select the marks of 3rd, which are present at the 2nd index of the list, and then with the help of the assignment operator, assign the new marks to it. Then the code returns the updated list of marks of a student.

[90, 74, 85, 84, 65, 79, 60, 87, 69, 76]

This was the result we got by updating our list. We can just add the difference to the same to update the marks of the 3rd student.

studentMarks[2] = studentMarks[2] + 27

print(studentMarks)

In this set of lines above, we select the marks of the 3rd student, which are present at the 2nd index of the list, and then with the help of the assignment operator, add the difference to the marks of the 3rd student. Then the code returns the updated list of marks of a student.

[90, 74, 85, 84, 65, 79, 60, 87, 69, 76]

And you can see that we had an updated list of marks of students. We got the same result by both the patterns.

Replace Item in List: Using For Loop

If we want to replace the items in the list, another method to do so in python is by looping the list. The loop will check the condition of the list, and if it is met, then the execution of the inner body begins; otherwise, it is skipped by the python program. Let’s take the same example as the last method and implement the for loop method to replace the marks.

Suppose there is a list of marks of students according to their performance in the test. Now it has been decided that the students who got marks < 30 will be given grace marks of 20. So now, we need to update the list, and for that, we will use For Loop to iterate through the whole list and update the desired ones.

Now, let’s get started by creating a list in python that contains the marks of the students:

studentMarks =[90, 14, 58, 84, 15, 79, 20, 87, 69, 76]

Now we will code the list comprehension, which will iterate through the list, and we will check the condition for the grace marks to update the marks of failed students and provide them with the grace marks.

for i in range(len(studentMarks)):

if studentMarks [i] <= 30.00:

studentMarks [i] = studentMarks [i] + 20.00

print(studentMarks)

This block of code will traverse through the whole list and update the marks of only those students who obtained a score of less than 30. On completion, the code returns the updated list of student marks.

studentMarks =[90, 34, 58, 84, 35, 79, 40, 87, 69, 76]

Above is the list returned on the complete execution of the python code. As you can see, the marks of students who scored less than 30 are updated by adding 20 marks and providing them with the required grace marks.

Replace Item in List: Using List Comprehension

The listening comprehension method in python is the most effective method of finding and replacing the items in a list. This method is very useful for you if you want to make a new list of the existing ones with updated values.

Coding this method requires our python code to iterate through the whole list and check the condition we want to execute further, and if it is met, we replace the items in the list. We can say that its execution is similar to that of the for loop method to replace Item in List in Python. The only difference is that it creates a new list out of the existing list with updated values, and the code contains fewer lines than that of the for loop method. We will take the same example of for loop to execute this list comprehension method.

Suppose there is a list of marks of students according to their performance in the test. Now it has been decided that the students who got marks < 30 will be given grace marks of 20. So now, we need to update that, and for that, we will use the List Comprehension method to update the desired marks of students.

Now, let’s get started by creating a list that contains the marks of the students:

studentMarks =[90, 14, 58, 84, 15, 79, 20, 87, 69, 76]

Now we will code the list comprehension, which will iterate through the list, and we will check the condition for the grace marks to update the marks of failed students and provide them with the grace marks.

updatedMarks = [Marks + 20.00 if Marks <= 30.00 else Marks for Marks in studentMarks]

print(updatedMarks)

This block of code will traverse through the whole list and update the marks of only those students who obtained a score of less than 30. On completion, the code returns the updated list of student marks.

studentMarks =[90, 34, 58, 84, 35, 79, 40, 87, 69, 76]

Above is the list returned on the complete execution of the python code. As you can see, the marks of students who scored less than 30 are updated by adding 20 marks and providing them with the required grace marks.

Conclusion

In the above article, we discussed three methods to “Replace Item in List in Python”:

  • Replace Item in List: Using List Indexing :
    • Used when we want to replace the item at the given position in a list 
    • Easiest to implement

  • Replace Item in List: Using For Loop
    • Used when we want to replace the item at the given position in a list
    • Shorter than list indexing implementation

  • Replace Item in List: Using List Comprehension
    •  Used when we want to make a new list out of the existing one with updated values.
    • Its implementation is similar to that of for loop
    • Smaller than for loop implementation

Frequently Asked Questions

  • When and why do we need to Replace Items in the List in Python?
    •  Replacing items in the list can be used at several places. For example, if you are designing a menu for a restaurant using python and notice that an item has been misspelled, you can run through it and change that item in the list using the methods discussed above.

  • Which method should I use to Replace Items in the List in Python?
    • Replacing an item in a list in python can be done in several ways. Two of the most common ways are using list indexing or a for loop to replace an item. A list comprehension can be used to create a new list based on an existing list and make a change.

  • How To Implement List indexing Method Of Replacing an item in a list in python?
    • In this method of replacing an item in the list, we can choose the index of an element or the range of indexes of elements in the list. With the help of the assignment operator [=], we can replace the item at the given position in a list.

  • How To Implement For Loop Method Of Replacing an item in a list in python?
    • The loop will check the condition of the list, and if it is met, then the execution of the inner body begins; otherwise, it is skipped by the python program.

  • How To Implement For List Comprehension Of Replacing an item in a list in python?
    • Coding this method requires our python code to iterate through the whole list and check the condition we want to execute further, and if it is met, we replace the items in the list.

Also read How to Solve the Python Syntax Error: ‘Return Outside Function Problem’?

Replace Item in List in Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top