Reputation: 29
Suppose if my list has ["Apple","ball","caT","dog"]
then it should give me result 'ball
and 'dog'
.
How do I do that using re.findall()
?
Upvotes: 1
Views: 257
Reputation:
re.findall
is not what you want here. It was designed to work with a single string, not a list of them.
Instead, you can use filter
and str.islower
:
>>> lst = ["Apple", "ball", "caT", "dog"]
>>> # list(filter(str.islower, lst)) if you are on Python 3.x.
>>> filter(str.islower, lst)
['ball', 'dog']
>>>
Upvotes: 3
Reputation: 25964
Ask how to kill a mosquito with a cannon, I guess you can technically do that...
c = re.compile(r'[a-z]')
[x for x in li if len(re.findall(c,x)) == len(x)]
Out[29]: ['ball', 'dog']
(don't use regex for this)
Upvotes: 0
Reputation: 1122352
You don't need re.findall()
here, at all.
Use:
[s for s in inputlist if s.islower()]
The str.islower()
method returns True
if all letters in the string are lower-cased.
Demo:
>>> inputlist = ["Apple","ball","caT","dog"]
>>> [s for s in inputlist if s.islower()]
['ball', 'dog']
Use re.findall()
to find lowercased text in a larger string, not in a list:
>>> import re
>>> re.findall(r'\b[a-z]+\b', 'The quick Brown Fox jumped!')
['quick', 'jumped']
Upvotes: 5