1

I'm a newbie in python. I am making a script that will check if an item on the list is on a particular range, it will do some commands. and I have no idea how to make one. Im thinking of using an IF statement, but I think I'm wrong.

What I have in mind is something like:

list = [list of values] if ( an entry on a list is lower than x): #given that x is any number do this 
1
  • You want to check the entrys value? or index? Commented Oct 21, 2014 at 8:25

2 Answers 2

2

You can use min() on the list to return the smallest value. Thus:

if thelist and min(thelist) < x: print "There's something small in the list" 
Sign up to request clarification or add additional context in comments.

2 Comments

This unnecessarily traverses the entire list, and it raises ValueError when the list is empty.
@ChrisMartin Good points. I added a test for list validity. The performance penalty is of course true, but perhaps offset by min() being a built-in. I'll leave it, I think it has a clarity benefit at least.
0
if any(y < x for y in list): pass # do something 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.