Here some more examples
char = ['g:', 'l:', 'q:']
Using Replace
for i in range(len(char)):
char[i] = char[i].replace(':', '')
Using strip
for i in range(len(char)):
char[i] = char[i].strip(':')
Using a function
def list_cleaner(list_):
return [char_.strip(':') for char_ in list_]
new_char = list_cleaner(char)
print(new_char)
Using Generator function(adviced if you have a large piece of data)
def generator_cleaner(list_):
yield from (char_.strip(':') for char_ in list_)
# Prints all results in once
gen_char = list(generator_cleaner(char))
# Prints as much as you need, in this case only 8 chars
# I increase a bit the list so it makes more sense
char = ['g:', 'l:', 'q:', 'g:', 'l:', 'q:', 'g:', 'l:', 'q:']
# let's call our Generator Function
gen_char_ = generator_cleaner(char)
# We call only 8 chars
gen_char_ = [next(gen_char_) for _ in range(8)]
print(gen_char_)