Shyam
Shyam

Reputation: 387

How to remove unwanted elements from list using python

I have an input list:

list_1 = ['29','560001','08067739333','560037002','29AAACC0462F1Z0','55XX1XXX19','07S23X09','98561XXX1X9']

I have tried:

output_list = [i for i in list_1 if 'X' in i or i.isnumeric()==True]

Giving out with extra element '07S23X09' which is wrong:

output_list = ['29','560001','08067739333','560037002','55XX1XXX19','07S23X09','98561XXX1X9']

Expected output is the list with numbers and the elements with numbers and specific character X, else other elements should be discarded:

output_list = ['29','560001','08067739333','560037002','55XX1XXX19','98561XXX1X9']

Upvotes: 1

Views: 620

Answers (3)

user13824946
user13824946

Reputation:

This could be easily done with python regex like the following

import re

list_1 = ['29','560001','08067739333','560037002','29AAACC0462F1Z0','55XX1XXX19','07S23X09','98561XXX1X9']

l2 = re.sub('[a-zA-VY-Z]', "", str(list_1))  # delete unwanted characters which are lowercase and uppercase letters from a to v and Y-Z since we only need to preserve uppercase letter X

print(l2)

output

['29', '560001', '08067739333', '560037002', '29046210', '55XX1XXX19', '0723X09', '98561XXX1X9']

Upvotes: 0

Carlos Brandt
Carlos Brandt

Reputation: 61

How about: output_list = [i for i in list_1 if i.replace('X', '').isnumeric()==True]

You seem to want all the numeric ones, but are ok if it's an 'X' in there. So if you remove the X's and check for numeric, that would do the trick.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626893

You may use

import re
list_1 = ['29','560001','08067739333','560037002','29AAACC0462F1Z0','55XX1XXX19','07S23X09','98561XXX1X9']
rx = re.compile('[0-9X]+')
print ( [x for x in list_1 if rx.fullmatch(x)] )
# => ['29', '560001', '08067739333', '560037002', '55XX1XXX19', '98561XXX1X9']

See the Python demo.

With re.fullmatch('[0-9X]+', x), you only keep the items that fully consist of digits or X chars.

See ^[0-9X]+$ the regex demo.

NOTE: If there must be at least one digit in the string, i.e. if you want to fail and thus discard all items that are just XXX, you may use

^X*[0-9][0-9X]*$

Or, ^(?=X*[0-9])[0-9X]+$. See the regex demo.

Upvotes: 4

Related Questions