How do I find countries where the name has between 4 and 5 letters with regex in python list?

Question:

How can I generate a list with regex in python for countries with 4 or 5 letters?

names = ['Azerbaijan', 'Zimbabwe', 'Cuba', 'Cambodia', 'Somalia','Mali', 'Belarus', "Côte d'Ivoire", 'Venezuela', 'Syria', 'Kazakhstan', 'Austria', 'Malawi', 'Romania', 'Congo (Brazzaville)']

I was trying to do this but it returns an empty list:

import re

n = [w for w in names if re.findall(r'[A-Z]{4,6},', str(names))]

print(n)

Output:
[]

It is an exercise that’s why I can only do it with the module re.
Any help will be appreciate.

Asked By: tucomax

||

Answers:

if you need to use re then here is your answer find words of length 4 using regular expression

if you dont need to use re just do
n = [name for name in names if len(name) in (4,5)]

Answered By: brownie

You can use len(w).

>>> names = ['Azerbaijan', 'Zimbabwe', 'Cuba', 'Cambodia', 'Somalia','Mali', 'Belarus', "Côte d'Ivoire", 'Venezuela', 'Syria', 'Kazakhstan', 'Austria', 'Malawi', 'Romania', 'Congo (Brazzaville)']

>>> [w for w in names if w.isalpha() and (len(w) in range(4,6))]
['Cuba', 'Mali', 'Syria']

But if you want to solve it with regex you can use re.search and
If maybe you have numbers in list you can use walrus operator (:=) for python >= 3.8

names = ['1234', 'Azerbaijan', 'Cuba']
print([w for w in names if (tmp := re.search(r'[A-Za-z]{4,5}', w)) and (tmp.group(0) == w)])
# ['Cuba']

For python < 3.8 you can use try/except.

names = ['1234', 'Azerbaijan', 'Cuba']

res = []
for w in names:
    try :
        if re.search(r'[A-Za-z]{4,5}', w).group(0) == w:
            res.append(w)
    except AttributeError:
        continue
print(res)
# ['Cuba']
Answered By: I'mahdi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.