473,378 Members | 1,417 Online

# checking for negative values in a list

hi all,

i am new to python guys.
hope u will help me with this....

i have a list of numbers

say a = [1,-1,3,-2,4,-6]

how should i check for negative values in the list
Dec 17 '07 #1
3 27035
On Mon, 17 Dec 2007 06:20:23 -0800, vimal wrote:
i have a list of numbers

say a = [1,-1,3,-2,4,-6]

how should i check for negative values in the list
In [6]: a = [1, -1, 3, -2, 4, -6]

In [7]: any(n < 0 for n in a)
Out[7]: True

Ciao,
Marc 'BlackJack' Rintsch
Dec 17 '07 #2
i am new to python guys.
i have a list of numbers

say a = [1,-1,3,-2,4,-6]

how should i check for negative values in the list
I'm not sure if this is a homework problem, as it seems to be a
fairly simple programming problem whether you know Python or not.

If you're using 2.5 or more recent, you should be able to do
something like

if any(x < 0 for x in a):
yep()
else:
nope()

If "a" is small, you could do

if [x for x in a if x < 0]:
yep()
else:
nope()

Or you could write your own function:

def has_negatives(iterable):
for x in iterable:
if x < 0: return True
return False

if has_negatives(a):
yep()
else:
nope()

-tkc
Dec 17 '07 #3
thanks for your help Tim and Marc.
:)
Dec 17 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 7 by: Colin Brown | last post by: Python 2.3.3 Under Windows NT: Negative Int or Real -> sleeps for a long time Under Linux: Returns IOError: Invalid argument I consider both of these unfriendly results and expected the... 19 by: David Abrahams | last post by: Can anyone explain the logic behind the behavior of list slicing with negative strides? For example: >>> print range(10) I found this result very surprising, and would just like to see the... 26 by: Sterten | last post by: when I define int R; and then later access it with x=R;C=7; .... but x happens to be <0 or >99 , then the program's behavious becomes unpredictable. Is there a way to prevent this ? Is... 1 by: illegal.prime | last post by: So I see from the documentation here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp That the code uses the... 3 by: iler.ml | last post by: I am writing code that uses two third-party libraries. They both define same macro OP_ENCRYPT, and luckily for me, they both define it to 0. (In one include, it's '#define OP_ENCRYPT 0', in... 39 by: Frederick Gotham | last post by: I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,... 1 by: JJ | last post by: What's best practise in this situation: Upon submitting data input on a detailsview (bound to an objectdatasource), I need to check for a duplicate email address (in an sql database) and present... 20 by: Casey | last post by: Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including... 0 by: Gabriel Genellina | last post by: En Fri, 18 Apr 2008 12:23:08 -0300, Shawn Milochik