Skip to content

Not any Python

  • by

Using python Not any() will deal with values that aren’t explicitly True or False. You have to write a generator expression that tests your custom condition.

any(not i for i in l)

There is also the all function which does the opposite of what you want, it returns True if all are True and False if any are False. Therefore you can just do:

not all(l)

Not any Python example

A simple example code gets the list without a digit.

lst = ['abc123','qwerty','123','hello']

new_list = [item for item in lst if not any(char.isdigit() for char in item)]

print(new_list)

Output:

Not any Python

Alternatively, you can use the re regex module:

new_list = [item for item in lst if not re.search(r'\d', item)]

Do comment if you have any doubts or suggestions on this Python any() function topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading