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.
Python3
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.
Python3
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.
Python3
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.
Python3
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)