You ask for "if any element in the list is greater than x
". If you just want one element, you could just find the greatest element in the list using the max()
function, and check if it's larger than x
:
if max(list) > x:
...
There's also a one-liner you can do to make a list of all elements in the list greater than x
, using list comprehensions (here's a tutorial, if you want one):
>>> x = 22
>>> list = [10, 20, 30, 40]
>>> greater = [i for i in list if i > x]
>>> print(greater)
[30, 40]
This code generates a list greater
of all the elements in list
that are greater than x
. You can see the code responsible for this:
[i for i in list if i > x]
This means, "Iterate over list
and, if the condition i > x
is true for any element i
, add that element to a new list. At the end, return that new list."