Ishpreet
Ishpreet

Reputation: 5880

How to remove english alphabets from list in python

I have a list with some English text while other in Hindi. I want to remove all elements from list written in English. How to achieve that?

Example: How to remove hello from list L below?

L = ['मैसेज','खेलना','दारा','hello','मुद्रण']  

for i in range(len(L)):    
    print L[i]

Expected Output:

मैसेज    
खेलना    
दारा    
मुद्रण

Upvotes: 7

Views: 9815

Answers (4)

aroy
aroy

Reputation: 482

You can use isalpha() function

l = ['मैसेज', 'खेलना', 'दारा', 'hello', 'मुद्रण']
for word in l:
    if not word.isalpha():
        print word

will give you the result:

मैसेज
खेलना
दारा
मुद्रण

Upvotes: 9

Burhan Khalid
Burhan Khalid

Reputation: 174624

How about a simple list comprehension:

>>> import re
>>> i = ['मैसेज','खेलना','दारा','hello','मुद्रण']
>>> [w for w in i if not re.match(r'[A-Z]+', w, re.I)]
['मैसेज', 'खेलना', 'दारा', 'मुद्रण']

Upvotes: 2

Autonomy
Autonomy

Reputation: 21

You can use Python's regular expression module.

import re
l=['मैसेज','खेलना','दारा','hello','मुद्रण']
for string in l:
    if not re.search(r'[a-zA-Z]', string):
        print(string)

Upvotes: 0

Kirill
Kirill

Reputation: 8311

You can use filter with regex match:

import re
list(filter(lambda w: not re.match(r'[a-zA-Z]+', w), ['मैसेज','खेलना','दारा','hello','मुद्रण']))

Upvotes: 1

Related Questions