I have a long list like this: A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23']
How can I get only the letters part without numbers and make them another list?
>>> [''.join([l for l in x if l.isalpha()]) for x in xs]
['ABCd', 'Efhgh', 'dhAsadjkhdk']
Using re.sub, this is quite simple!
>>> strings = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23']
>>> import re
>>> [re.sub(r'[^A-Za-z]+', '', s) for s in strings]
['ABCd', 'Efhgh', 'dhAsadjkhdk']
Want the numbers too?
>>> [re.sub(r'[^\d]+', '', s) for s in strings]
['123', '345', '23']
>>> A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23']
>>> B = list(map(lambda x: ''.join([letter for letter in x if letter.isalpha()]), A))
>>> B
['ABCd', 'Efhgh', 'dhAsadjkhdk']
filter() would have been a better choice than map():
>>> A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23']
>>> B = [''.join(filter(str.isalpha, a)) for a in A]
>>> B
['ABCd', 'Efhgh', 'dhAsadjkhdk']
This, of course, is basically identical to @chrisaycock's answer.
If you'd like to eliminate duplicate entries, use a set:
>>> A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23', 'ABCd 95']
>>> B = set(''.join(filter(str.isalpha, a)) for a in A)
>>> B
{'Efhgh', 'dhAsadjkhdk', 'ABCd'}
If you'd like to eliminate duplicate entries but you need to preserve the ordering of the original list, sort a set by index:
>>> A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23', 'ABCd 95']
>>> B = [''.join(filter(str.isalpha, a)) for a in A]
>>> B
['ABCd', 'Efhgh', 'dhAsadjkhdk', 'ABCd']
>>> C = sorted(set(B), key=lambda x: B.index(x))
>>> C
['ABCd', 'Efhgh', 'dhAsadjkhdk']
or avoid an intermediate list and set by doing it manually:
>>> A = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23', 'ABCd 95']
>>> B = []
>>> for a in A:
... b = ''.join(filter(str.isalpha, a))
... if b not in B:
... B.append(b)
...
>>> B
['ABCd', 'Efhgh', 'dhAsadjkhdk']
filter(str.isalpha, s) and it will automatically return a string without you having to join because filter does a smart type-check.list's order.>>> values = ['ABCd 123', 'Efhgh 345', 'dhAsadjkhdk 23']
>>> [value.split()[0] for value in values]
['ABCd', 'Efhgh', 'dhAsadjkhdk']