6

I'm trying to check if a specific element in the list is greater than a set value.

So

x=22
list=[10,20,30] 

# check if anything in list is greater than x
# do something to the list

I'm just not sure what commands to use in this scenario to check every list element or if it's even possible in one line.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
user195491
  • 71
  • 1
  • 1
  • 3
  • Welcome to the site. Unfortunately, it's not clear what you want. Please clarify: Do you just want to know if the list contains an element greater than the given value? Or a list of all elements greater? Or if a specific element (say the 3rd element out of 5) is greater? – GreenMatt Oct 10 '18 at 19:54

4 Answers4

20

Use any, Python's natural way of checking if a condition holds for, well, any out of many:

x = 22 
lst = [10, 20, 30]  # do NOT use list as a variable anme

if any(y > x for y in lst):
    # do stuff with lst

any will terminate upon the first truthy element of the iterable it is passed and, thus, not perform any spurious iterations which makes it preferable to max or list comprehension based approaches that have to always iterate the entire list. Asymptotically however, its time complexity is, of course, still linear.

Also, you should not shadow built-in names like list.

user2390182
  • 72,016
  • 6
  • 67
  • 89
4

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."

Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
1

You could use filter and filter your list for items greater than 22, if the len of this filtered list is > 0 that means your original list contains a value larger than 22

n = 22
lst = [10,20,30]

if len(list(filter(lambda x: x > n, lst))) > 0:
    # do something to lst
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20
0

List comprehension would suit your use case.

In [1]: x=22

In [2]: l=[10,20,30]

In [3]: exist = [n for n in l if n > x]

In [4]: if exist:
   ...:     print "elements {} are greater than x".format(exist)
   ...: 
elements [30] are greater than x

If your list is very long, use generator so that you do not have to iterate through all elements of the list.

In [5]: exist = next(n for n in l if n > x)

In [6]: if exist:                          
    print "Element {} is greater than x".format(exist)
   ...:     
Element 30 is greater than x
clony
  • 136
  • 2