2

I have a list of strings that I am iterating through. With each element/iteration I am trying to take the string and remove all non-alphabetic characters(numbers, punctuations, spaces, and more). However, when using a nested for-loop to access the individual characters and using replace function it is not working.

import string

new_war = ['I am the new king of Mississippi', 'next one you know', 'i got a family to feed']
print(new_war)

alphabet = string.ascii_uppercase + string.ascii_lowercase

def remove_char(lst):
    for word in lst: #takes the individual string/element from the list
        for chars in word: #goes through each individual character in the word element
            if chars not in alphabet: #check to see if that character is in the alphabet(upper and lower)
                word = word.replace(chars, "") #should take the given string, remove all occurrences of that char in the string via "", 

remove_char(new_war)
3
  • The method itself is working quite well. What exactly do you mean by "not working"? Commented Mar 12, 2022 at 18:33
  • Rarely a good idea to modify a list as you iterate over it. Also, chars should be singular. Commented Mar 12, 2022 at 18:33
  • 2
    word = word.replace(chars, "") This assigns to a new variable named word. It does not modify the original list entry. Commented Mar 12, 2022 at 18:34

2 Answers 2

3

If you want to modify the original list items, then you have to operate on them directly.

def remove_char(lst):
    for i in range(len(lst)):
        for chars in lst[i]:
            if chars not in alphabet:
                lst[i] = lst[i].replace(chars, "")

Note how this code refers to lst[i], not a local variable.

Sign up to request clarification or add additional context in comments.

Comments

1

As already pointed out when you use

for word in lst:

you get the value "word" (edit: a reference to the same immutable string), which is a string. Basically a copy (edit : new reference) of it, for that one iteration of the loop. That means whatever you do to "word" then, it applies to (edit : a new immutable string, to which you re-reference your variable, which is when a new allocation must be made) that copy, not what is inside your list.

To make things more practical, I suggest you use the enumerate() function of python.

As such :

import string

new_war = ['I am the new king of Mississippi', 'next one you know', 'i got a family to feed']

alphabet = string.ascii_uppercase + string.ascii_lowercase


def remove_char(lst):
    for key, word in enumerate(lst):  # takes the individual string/element from the list
        for chars in word:  # goes through each individual character in the word element
            if chars not in alphabet:  # check to see if that character is in the alphabet(upper and lower)
                lst[key] = word.replace(chars,
                                        "")  # should take the given string, remove all occurrences of that char in the string via "",


remove_char(new_war)

print(new_war)

1 Comment

I'll edit to be more technically correct, I guess. but the "Basically" here was supposed to imply that there was more to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.