Replace element in a list python for freshers python interview question #shorts #coding #interview

preview_player
Показать описание
Python interview question
List in python used multiple items in a single variable.
In this python short tutorial let’s see how to replace an element in a list.
We will look into 2 examples.
First example let’s try replacing the element directly by using index position. We change the value of index 1 from value 3 to 9.
You can see that the value is changed from 3 to 9
In the second example we will change all the occurrences of 1 to 10.
To achieve it lets use enumerate. Enumerate is a function which takes in an iterable and returns the index along with the value. In our example list is an iterable.
We compare the value at each index and if we find it to be 1 we replace the index to value 10
You can see we have replaced all ones to 10
This is one of the solutions there are many optimized methods such as inline statements and lamda function.

Using Visual Studio Code editor

#shorts #python #Coding #interview #tutorial #VSCode #freshers #2022
Рекомендации по теме
Комментарии
Автор

We can use insert function like these
l.insert(0, 10), here the first argument is a index number and the second argument is the value of index you want replace with another value.

meetmulani