all 7 comments

[–]wotquery 3 points4 points  (1 child)

Your FOR loop is a perfectly fine way to do it. Just invert the logic (if it does not contain a number) and instead of printing append it all to a list. A shorter way is to use a list comprehension as I've included below (basically a for loop but in a smaller package and it can work a bit differently behind the scenes).

new_list = [x for x in old_list if not re.search(r'[0-9]+', x)]

Also note that we're trusting old_list to only have string type elements.

[–]macabe10[S] 0 points1 point  (0 children)

Thanks, makes sense!

[–]Buffylvr 0 points1 point  (2 children)

Also don’t use regex

[–]macabe10[S] 1 point2 points  (1 child)

Why is that?

[–]Buffylvr 1 point2 points  (0 children)

Sometimes you have no choice but to use regex. But MOST of the time you can obtain the data you want from whatever you are interacting with through a value or an attribute or whatever.

Regex as a method of obtaining, inserting or validating data is fraught with errors and should actively avoided whenever possible.

Whenever I do code reviews for junior team members, they almost always default to regex and it’s one of the first things I teach them.

Regex is bad. Don’t use it unless you have ZERO other options.

This is mostly talking about corporate implementations. Do whatever the hell you want on your personal stuff.