• Register
1 vote
29 views

Problem:

I have a simple and basic level question to you guy. How can I replace an element in a list python? Yeah, it might be a silly question but need to be answered badly. I am struggling with this problem for the last thirty minutes almost. But I am unable to sort out this thing. Your kind help is much needed.
Thanks

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

You can easily replace any element in a list of python by using a for-loop. You’ll check the particular element in the for-loop and replace it. Let’s see how to do it.

>>> x= [1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
>>> for n, i in enumerate(x):
...   if i == 5:
...      x[n] = 10
...
>>> x
[1, 2, 3, 4, 10, 10, 4, 3, 2, 1]

If you want to replace more than one element in a list you can use a dictionary. Let’s check the program below:

x = [1, 2, 3, 4, 1, 5, 3, 2, 1]
dic = {1:5, 2:10, 3:'gavin'}

print([dic.get(n, n) for n in x])

> [5, 10, 'gavin', 4, 5, 5, 'gavin', 10, 5]

This may help you. Thanks

13 9 6
94,260 points

Related questions

1 vote
2 answers 43 views
43 views
Problem: I am very noob at programming language so I am doing my pracice on python. I tried to to replace multiple characters in a string in python. So I write this code: tes_str = "abababababababba" print("The main string is : " + str(tes_str)) res = tes_str('a', '%temp%')('b', ... = tes_str('a', '%temp%')('b', 'a')('%temp%', 'b') TypeError: 'str' object is not callable Can anyone help me out?
asked Apr 6, 2020 LizzyM 6.1k points
0 votes
1 answer 6 views
6 views
Problem: Please help me with my query as to how to replace an item in a list python?
asked Apr 2 Sana8989 8.9k points
1 vote
1 answer 106 views
106 views
Problem: I was trying to check if an element does exist in a list using a pyhton program: primary_list = [ 10, 12, 16, 6, 20, 33 ] a = input("Please enter a number to check if this exists in the list: ") print("Checking if input exists in this list : ") for ... not giving any output that is right. What should I do to fix this and how to check if an element does exist in a list. Thanks in advance.
asked May 16, 2020 samhaz 5k points
1 vote
1 answer 11 views
11 views
Problem: How to find the index of an item in a list ?
asked Mar 6 Raj Tyagi 14.4k points
0 votes
1 answer 8 views
8 views
Problem: How to I Solve this: find an element in a list python HELP! I need help understanding this: find an element in a list python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 5 Mashhoodch 13k points
0 votes
1 answer 10 views
10 views
Problem: how to I Solve this: error: removing an element from a list python HELP! I need help understanding this: removing an element from a list python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 11 Mashhoodch 13k points
0 votes
1 answer 2 views
2 views
Problem: I need someone’s assistance to solve my problem .. How to remove element from list in python
asked 11 hours ago Ifra 34.7k points
0 votes
1 answer 6 views
6 views
Problem: Is there any difference between del, remove, and pop methods to remove an element from a list?
asked Apr 16 sumaiya simi 43.9k points
0 votes
1 answer 4 views
4 views
Problem: Python check if element is in list.
asked Apr 11 Dan phillip 4.8k points
0 votes
1 answer 8 views
8 views
Problem: How to I Solve this: finding index of element in list python HELP! I need help understanding this: finding index of element in list python Can someone please help solve this, I am new in python(). Someone have idea to solve this thanks!
asked Mar 5 Mashhoodch 13k points