Python | Check if any item in the list meets the condition

Method # 1: Using List Comprehension
This problem can be easily solved with loops. But this method provides one liner for solving this problem. List comprehension simply checks any element that satisfies the condition.

# Python3 code to demonstrate how it works
# Check if any item in the list satisfies the condition
# Using the comprehension list

 
# initializing list

test_list = [ 4 , 5 , 8 , 9 , 10 , 17 ]

  
# print list

print ( "The original list:" + str (test_list))

 
# Check if any item in the list meets the condition
# Using the comprehension list

res = True in (ele & gt;  10 for ele in test_list)

 
# Print result

print ( "Does any element satisfy specified condition?:" + str (res ))

Output:

 The original list: [4, 5, 8, 9, 10, 17] Does any element satisfy specified condition? : True 

Method # 2: Using any()
This is the most common method for solving this particular problem. In this, we are just using a built-in function extended by the Python library to solve this task. It checks any element that matches the condition and returns True if it finds any one element.

# Python3 code to demonstrate how it works
# Check if any element in the list meets the condition
# Using any ()

 
# init list

test_list = [ 4 , 5 , 8 , 9 , 10 , 17 ]

  
# print list

print ( "The original list:" + str (test_list))

 
# Check if any item in the list meets the condition
# Using any ()

res = any (ele & gt;  10 for ele in test_list)

 
# Print result

print ( "Does any element satisfy the specified condition?:" + str (res ))

Output:

 The original list: [4, 5, 8, 9, 10, 17] Does any element satisfy specified condition? : True