• Register
1 vote
27 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,240 points

Related questions

1 vote
2 answers 38 views
38 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
1 vote
1 answer 52 views
52 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 24 views
24 views
Problem: Hello Kodlogers, Myself is a high school student. I’ve started learning Python programming recently. I am wanting to replace an item in a list python. Let’s say I have a list of integers like: my_list = [10, -15, 1, 8, 0, 9, -5, 13, -1, 5] ... or 1. Negatives values with -1, positive values with 1 and leave 0 untouched. Is it doable? If doable, how? Thanks for your help, mate.
asked Apr 4, 2020 Gavin 15.3k points
0 votes
2 answers 12 views
12 views
Problem: How a list is the most flexible data structure in Python. Whereas, a 2D list which is commonly known as a list of lists, is a list object where every item is a list itself - for example: [[1,2,3], [4,5,6], [7,8,9]].
asked Jan 26 Mashhoodch 9.9k points
1 vote
1 answer 45 views
45 views
Problem: I have 2 list that consist of some name and some numbers: list_1 = ['samhaz', 'porto', 'chris', 'evans'] list_2 = ['33', '14', '26', '30'] I tried to add this two list together(the names with the numbers). For that I wrote a python program: ... ' object has no attribute 'add' What is the problem in my program and how can I add those 2 list(names and numbers) together. Thanks in advance.
asked May 13, 2020 samhaz 5k points
1 vote
1 answer 45 views
45 views
Problem: Recently I started learning python and just about to know more about list and tuple in python. I am a little bit known to the list and tuple and I can add or subtracts elements in a list. I wonder how to multiply all elements in a list python? I tried it in the adding process but it didn’t work for me. Is it doable? Could anybody please show me the way to do it?
asked Apr 3, 2020 Gavin 15.3k points
0 votes
1 answer 5 views
5 views
Problem: I have an integer and a list. I would like to make a new list of them beginning with the variable and ending with the list. Writing a + list I get errors. The compiler handles a as integer, thus I cannot use append, or extend either. How would you do this?
asked Feb 19 Mashhoodch 9.9k points
0 votes
1 answer 7 views
7 views
Problem: For a list ["foo","bar","baz"]and a list item "bar", what is the cleanest way to get its index (1) in python?
asked 3 days ago sasha 13.2k points
0 votes
1 answer 12 views
12 views asked Oct 8, 2020 jagruthi 680 points
0 votes
1 answer 4 views
4 views
Problem: I have a list like this: a = [[4.0, 4, 4.0], [3.0, 3, 3.6], [3.5, 6, 4.8]] I want an outcome like this (EVERY first element in the list): 4.0, 3.0, 3.5 I tried a[::1][0], but it doesn't work
asked Feb 23 Mashhoodch 9.9k points