473,378 Members | 1,417 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,378 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 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

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):

If "a" is small, you could do

if [x for x in a if x < 0]:

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):

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

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...
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...
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...
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...
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...
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,...
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...
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...
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...
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...
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...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.