1

I have the list lst= ["is ", "star,", "the-"] and I want to remove ',', '-' without using re.

I used the below and it works but I wondered if there is something simpler:

words = []
index = 0
length = 0

for char in lst:
    for i, c in enumerate(char):
        if c.isalpha():
            if length == 0:
                index = i
            length += 1
        else:
            word = char[index:index + length]
            words.append(word)
            length = 0
print(words)
eponkratova
  • 467
  • 7
  • 20
  • Does this answer your question? [Most efficient way to remove multiple substrings from string?](https://stackoverflow.com/questions/30606124/most-efficient-way-to-remove-multiple-substrings-from-string) The question itself, not the answer... – Tomerikoo Dec 02 '20 at 10:23
  • lst = [x.replace(",").replace("-") for x in lst] – saph_top Dec 02 '20 at 10:24
  • Or better: [How to replace multiple substrings of a string?](https://stackoverflow.com/questions/6116978/how-to-replace-multiple-substrings-of-a-string) (23 answers...) – Tomerikoo Dec 02 '20 at 10:25
  • No, if you notice in my case, I need to replace several symbols, while the replace allows replacing one unless I don't do replace on replace which I don't want. – eponkratova Dec 02 '20 at 10:28
  • Can you, please, suggest merging my question, again? As I cannot delete the question. – eponkratova Dec 02 '20 at 10:35
  • If any gold-badger happens to pass by here, this is a much more accurate duplicate: [Remove all special characters, punctuation and spaces from string](https://stackoverflow.com/questions/5843518/remove-all-special-characters-punctuation-and-spaces-from-string?noredirect=1&lq=1) – Tomerikoo Dec 02 '20 at 10:49

3 Answers3

1

Hope this helps you:

lst = ["is ", "star,", "the-"] 
lst = [''.join(e for e in f if e.isalpha()) for f in lst] 
print(lst)

Output:

['is', 'star', 'the']
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
AziMez
  • 2,014
  • 1
  • 6
  • 16
0

If you are only interested in lower case characters, you could use python's in built ord method.

for idx, word in enumerate(words):
    new_word = ""
    for char in word:
        if char == " " or 97 <= ord(char) <= 122:
            new_word += char
    words[idx] = new_word
ATIF ADIB
  • 571
  • 5
  • 10
  • it works, thank you! I will accept you answer in 6 min. – eponkratova Dec 02 '20 at 10:28
  • It would be alot more clear to do a simple [`char.isalpha()`](https://docs.python.org/3/library/stdtypes.html#str.isalpha) instead of `97 <= ord(char) <= 122` – Tomerikoo Dec 02 '20 at 10:34
  • yes but i think the requirement is lower case characters and isalpha would be true for both upper and lower case characters – ATIF ADIB Dec 02 '20 at 10:47
  • Well there are no examples with upper case letters but the requirement is to remove special characters so I would guess all letters should be accepted... – Tomerikoo Dec 02 '20 at 10:52
0
words = []
for word in lst:
    #clean_word: loop the word and check every single value if it is alphanumeric, append and pass if it is a special characters or spaces. It will become a list since we do list comprehension (['i', 's']) and join them to become a string ('is').
    clean_word = [letter for letter in word if letter.isalnum()]
    words.append(''.join(clean_word))
print (words)
bonifacio_kid
  • 633
  • 5
  • 8