Open In App

Remove all Alphanumeric Elements from the List – Python

Alphanumeric elements consist of only alphabetical and numerical characters. Any of the special characters are not included in alphanumeric elements.

This article is going to show you multiple ways to remove all the alphanumeric elements from a List of strings using Python.



Example 1:
Input: ['a','b', '!', '1', '2', '@', 'abc@xyz.com']
Output: ['!', '@', 'abc@xyz.com']

Example 2:
Input: ['A+B', '()', '50', 'xyz', '-', '/', '_', 'pq95', '65B']
Output: ['A+B', '()', '-', '/', '_']

How to Remove all Alphanumeric Elements from the List in Python using isalnum()

isalnum() is a Python string method that returns True If all the characters are alphanumeric.




l1 = ['A+B', '()', '50', 'xyz', '-', '/', '_', 'pq95', '65B']
l2 = list()
  
for word in l1:
   if not word.isalnum():
        l2.append(word)
  
print(l2)

Output:



['A+B', '()', '-', '/', '_']

Time Complexity: O(N)
Auxiliary Space: O(N)

How to Remove all Alphanumeric Elements from the List in Python using isalpha() and isnumeric()

isalpha() is a Python string method that returns True if all characters are alphabets.

isnumeric() is a Python string method that returns True if all characters are numbers.




l1 = ['A+B', '()', '50', 'xyz', '-', '/', '_', 'pq95', '65B']
l2 = list()
  
for word in l1:
   for char in word:
       if not (char.isalpha() or char.isnumeric()):
           l2.append(word)
           break
  
print(l2)

Output:

['A+B', '()', '-', '/', '_']

Time Complexity: O(N)
Auxiliary Space: O(N)

How to Remove all Alphanumeric Elements from the List in Python using ASCII Value or Range

ASCII  stands for American Standard Code for Information Interchange is a common data encoding format for data communication. According to this standard every letter, number, and special character has associated ASCII values.




l1 = ['A+B', '()', '50', 'xyz', '-', '/', '_', 'pq95', '65B']
l2 = list()
  
for word in l1:
   for char in word:
       if not ((char >= 'A' and char <= 'Z'
               or 
               (char >= 'a' and char <= 'z')
               or
               (char >= '0' and char <= '9')):
           l2.append(word)
           break
  
print(l2)

Output:

['A+B', '()', '-', '/', '_']

Time Complexity: O(N)
Auxiliary Space: O(N)

How to Remove all Alphanumeric Elements from the List in Python using Regular Expression

Regular Expression or RegEx is a tool useful for pattern-based string matching. The python standard library already provides a re module for using RegEx in Python easily and effectively.




import re
  
l1 = ['A+B', '()', '50', 'xyz', '-',
      '/', '_', 'pq95', '65B']
l2 = list()
  
for s in l1:
  if re.findall(r'''[]_`~!@#$%^&*()-+={[}}|\:;"'<,>.?/]''',s):
    l2.append(s)
print(l2)

Output:

['A+B', '()', '-', '/', '_']

Time Complexity: O(N)
Auxiliary Space: O(N)


Article Tags :