0

I am trying to make a program where it takes in user input and spits out the input without the letters. I'm really confused on why this isn't working.

numText = input("Give me a list of characters: ") # Gets user input of characters
numList = list(numText) # Turns ^ into list

for char in numList: # Every character in the list
    if char.isalpha(): # Checks to see if the character is a letter
        numList.remove(char) # Removes the letter from the list

print(numList) # Prints the new list without letters
input("") # Used so the program doesnt shut off

I know this is really dumb so im sorry for asking.

  • FYI, If you are okay printing the input as a *string* instead of a list, then you can replace lines 2-7 in your script with `print(''.join(s for s in numText if not s.isalpha()))`. – Ray Toal Nov 11 '18 at 22:36

1 Answers1

6

You should not iterate and remove at the same time, instead use a list comprehension.

numText = input("Give me a list of characters: ") # Gets user input of characters

numList = [char for char in numText if not char.isalpha()]

print(numList) # Prints the new list without letters
input("")
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76