473,382 Members | 1,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 27051
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 <Shawn@Milochik.comescribió: A dictionary with keys is perfectly reasonable. But a *list* of values has to be searched linearly for every...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.