How to use List Indexing In Python
We can access items of the list using indexing. This is the simplest and easiest method to replace values in a list in python. If we want to replace the first item of the list we can di using index 0. Here below, the index is an index of the item that we want to replace and the new_value is a value that should replace the old value in the list.
Syntax: l[index]=new_value
Code:
Python3
# Replace Values in a List using indexing # define list l = [ 'Hardik' , 'Rohit' , 'Rahul' , 'Virat' , 'Pant' ] # replace first value l[ 0 ] = 'Shardul' # print list print (l) |
Output:
['Shardul', 'Rohit', 'Rahul', 'Virat', 'Pant']
How to Replace Values in a List in Python?
In this article, we are going to see how to replace the value in a List using Python. We can replace values in the list in serval ways. Below are the methods to replace values in the list.
- Using list indexing
- Using for loop
- Using while loop
- Using lambda function
- Using list slicing
Contact Us