+ 20

Lists

Imagine you have one list with multiple characters, like this: List = ['a', 'b', 'c', 'g', 'i', 'a', 'a'] Its possible i change all a in list without know the position of all a? If yes how? Thanks!

1st May 2019, 4:02 PM
Alix Carmo
Alix Carmo - avatar
38 Réponses
+ 23
just for fun: lst = ['a', 'b', 'c', 'g', 'i', 'a', 'a'] print(lst) lst = [char.replace('a', 'X') for char in lst] print(lst)
2nd May 2019, 8:45 AM
Lothar
Lothar - avatar
+ 11
hey man here is a way to change it without using loops and all those complicated map functions lis = ['a', 'b', 'c', 'g', 'i', 'a', 'a'] lis = "".join(lis) lis = lis.replace("a", "") lis = list(lis) print(lis) >>> ['b', 'c', 'g', 'i'] you change it into strings first and use the replace function to change all 'a' into nothing. then change it back to lists.
3rd May 2019, 10:24 AM
Shen Bapiro
Shen Bapiro - avatar
+ 5
Hubert Dudek after all help and with my zero experience the fastest is list(map({'a', 'new_value', list)) But i get confused when i use it and so im using Lst=["a", "b", "c"] For i in range(len(lst)) : If lst[i] == 'a' : Lst[i] = '' Print (lst) Its easier for me read and use this than the other one using map
4th May 2019, 5:14 PM
Alix Carmo
Alix Carmo - avatar
+ 4
Alix Carmo you can just specify mapping with values to be replaced in dictionary and then use map and lambda, see code here: https://code.sololearn.com/c2r4R7OsZfIb/?ref=app
1st May 2019, 11:55 PM
Hubert Dudek
Hubert Dudek - avatar
+ 4
Lists are used to store multiple items in a single variable.
21st Nov 2021, 3:47 AM
Vaibhav
Vaibhav - avatar
+ 3
This will work list(map({'a', 'new_value'},List)) Get more stuffs on freeCodecamp
1st May 2019, 5:33 PM
RISHABH MISHRA
RISHABH MISHRA - avatar
+ 3
Diego Biraj a bit improved version: Dict = {'a':'b'} List = ['a', 'b', 'c'] print(list(map(lambda x: Dict.get(x, x), List)))
1st May 2019, 7:34 PM
Hubert Dudek
Hubert Dudek - avatar
+ 3
ok so which method is the fastest?
3rd May 2019, 1:06 PM
Hubert Dudek
Hubert Dudek - avatar
+ 3
Hubert Dudek - Who has asked the question must also solve the problem. Are you going to do some speed tests? ;-)
16th May 2019, 10:46 AM
Lothar
Lothar - avatar
+ 3
Gowtham Kannan, this answer has no relation to the question - or does it? in addition, the code is syntactically incorrect.
20th Jul 2020, 1:58 PM
Lothar
Lothar - avatar
+ 2
try also: list(map({'a', 'new_value'},List))
1st May 2019, 4:34 PM
Hubert Dudek
Hubert Dudek - avatar
+ 2
Hubert Dudek TypeError: 'set' object is not callable.
1st May 2019, 4:38 PM
Diego
Diego - avatar
+ 2
hi, this will not work for me: list(map({'a', 'new_value'},List)) map does need a function as first argument - doesn‘t it?
2nd May 2019, 4:56 AM
Lothar
Lothar - avatar
+ 1
Thank you a lot!
1st May 2019, 4:08 PM
Alix Carmo
Alix Carmo - avatar
+ 1
Whattt... I didnt get that Hubert Dudek
1st May 2019, 7:37 PM
Alix Carmo
Alix Carmo - avatar
+ 1
seq = ["a", "b", "c", "g", "i", "a", "a"] seq = [*"".join(seq).replace("a", "A")] ,
16th May 2019, 10:25 AM
alex_shchegretsov
alex_shchegretsov - avatar
+ 1
very fun story
30th Oct 2020, 6:40 PM
Alex Saltykov
Alex Saltykov - avatar
+ 1
Praline Dheula , you should not post your question in an existing post of someone else. if this person will decide to delete his post, your question and ALL possible answers are then also deleted. To your question: do you want to search the source code during you develop the program or do you want to search when the program is executed and running?
18th Jan 2021, 6:20 PM
Lothar
Lothar - avatar
+ 1
Lothar Hi! Thanks for the advice :) I am not able to run a whole program yet so I'll go with during the development!( Please )
1st Feb 2021, 8:49 PM
Praline Dheula
Praline Dheula - avatar
+ 1
Lst=['a','b','a','c','a','d','a'] For x in range(len(lst)+1): If x==a: Lst[x]="@" Print (lst)
14th Apr 2021, 3:24 PM
Mayank Hemnani
Mayank Hemnani - avatar