+ 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!
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)
+ 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.
+ 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
+ 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
+ 4
Lists are used to store multiple items in a single variable.
+ 3
This will work
list(map({'a', 'new_value'},List))
Get more stuffs on freeCodecamp
+ 3
ok so which method is the fastest?
+ 3
Hubert Dudek - Who has asked the question must also solve the problem. Are you going to do some speed tests? ;-)
+ 3
Gowtham Kannan, this answer has no relation to the question - or does it? in addition, the code is syntactically incorrect.
+ 2
try also:
list(map({'a', 'new_value'},List))
+ 2
Hubert Dudek
TypeError: 'set' object is not callable.
+ 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?
+ 1
Thank you a lot!
+ 1
Whattt... I didnt get that Hubert Dudek
+ 1
seq = ["a", "b", "c", "g", "i", "a", "a"]
seq = [*"".join(seq).replace("a", "A")]
,
+ 1
very fun story
+ 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?
+ 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 )
+ 1
Lst=['a','b','a','c','a','d','a']
For x in range(len(lst)+1):
If x==a:
Lst[x]="@"
Print (lst)