How to Replace Values in a List in Python? Last Updated : 04 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Replacing values in a list in Python can be done by accessing specific indexes and using loops. 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 several ways. The simplest way to replace values in a list in Python is by using list indexing.Let’s take an example of replacing values in a list using list indexing. Python a = [10, 20, 30, 40, 50] # Replacing 30 to 99 a[2] = 99 print(a) Output[10, 20, 99, 40, 50] Let’s see other different methods to Replace Values in a List.Table of ContentUsing For LoopUsing While LoopUsing Lambda FunctionUsing Lambda FunctionIn this method, we use lambda and map function to replace the value in the list. map() is a built-in function in python to iterate over a list without using any loop statement. A lambda is an anonymous function in python that contains a single line expression. Here we gave one expression as a condition to replace value. Python a = [10, 20, 30, 40, 50] b = list(map(lambda x: 99 if x == 30 else x, a)) print(b) Output[10, 20, 99, 40, 50] Using For LoopWe can use for loop to iterate over the list and replace values in the list. We first find values in the list using for loop and if condition and then replace it with the new value. Python a = [10, 20, 30, 40, 50] for i in range(len(a)): if a[i] == 30: a[i] = 99 print(a) Output[10, 20, 99, 40, 50] Using While LoopWe can also use a while loop to replace values in the list. While loop does the same work as for loop. In the while loop first, we define a variable with value 0 and iterate over the list. If value matches to value that we want to replace then we replace it with the new value. Python a = [10, 20, 30, 40, 50] i = 0 while i < len(a): if a[i] == 30: a[i] = 99 i += 1 print(a) Output[10, 20, 99, 40, 50] Comment More infoAdvertise with us Next Article How to Replace Values in a List in Python? R rushi_javiya Follow Improve Article Tags : Python python-list Practice Tags : pythonpython-list Similar Reads How to Read Text File Into List in Python? In Python, reading a text file into a list is a common task for data processing. Depending on how the file is structuredâwhether it has one item per line, comma-separated values or raw contentâdifferent approaches are available. Below are several methods to read a text file into a Python list using 2 min read How to Change Values in a String in Python The task of changing values in a string in Python involves modifying specific parts of the string based on certain conditions. Since strings in Python are immutable, any modification requires creating a new string with the desired changes. For example, if we have a string like "Hello, World!", we mi 2 min read How to Remove Item from a List in Python Lists in Python have various built-in methods to remove items such as remove, pop, del and clear methods. Removing elements from a list can be done in various ways depending on whether we want to remove based on the value of the element or index.The simplest way to remove an element from a list by i 3 min read Pandas Replace Multiple Values in Python Replacing multiple values in a Pandas DataFrame or Series is a common operation in data manipulation tasks. Pandas provides several versatile methods for achieving this, allowing you to seamlessly replace specific values with desired alternatives. In this context, we will explore various approaches 5 min read How to replace a word in excel using Python? Excel is a very useful tool where we can have the data in the format of rows and columns. We can say that before the database comes into existence, excel played an important role in the storage of data. Nowadays using Excel input, many batch processing is getting done. There may be the requirement o 3 min read Replacing column value of a CSV file in Python Let us see how we can replace the column value of a CSV file in Python. CSV file is nothing but a comma-delimited file. Method 1: Using Native Python way Using replace() method, we can replace easily a text into another text.  In the below code, let us have an input CSV file as "csvfile.csv" and be 2 min read Replace Commas with New Lines in a Text File Using Python Replacing a comma with a new line in a text file consists of traversing through the file's content and substituting each comma with a newline character. In this article, we will explore three different approaches to replacing a comma with a new line in a text file. Replace Comma With a New Line in a 2 min read How to search and replace text in a file in Python ? In this article, we will learn how we can replace text in a file using python. Method 1: Searching and replacing text without using any external module Let see how we can search and replace text in a text file. First, we create a text file in which we want to search and replace text. Let this file b 5 min read Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is 2 min read Python - Remove empty value types in dictionaries list Sometimes, while working with Python dictionaries, we require to remove all the values that are virtually Null, i.e does not hold any meaningful value and are to be removed before processing data, this can be an empty string, empty list, dictionary, or even 0. This has applications in data preproces 7 min read Like