By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

is there a better way to check an array?

P: n/a
I want to check if a value is in an array. I'm currently doing it as
follows, but I just don't like this way of doing it. It seems
unpythonic.

fieldIsRequired = true
try:
notRequiredAry.index(k)
fieldIsRequired = false
except ValueError:
pass

# throw expception if field is required and is empty
if(product[k] == '' and fieldIsRequired):
raise GMError(k + ' is required')

Sep 1 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
jdonnell wrote:
I want to check if a value is in an array. I'm currently doing it as
follows, but I just don't like this way of doing it. It seems
unpythonic.

fieldIsRequired = true
try:
notRequiredAry.index(k)
fieldIsRequired = false
except ValueError:
pass

# throw expception if field is required and is empty
if(product[k] == '' and fieldIsRequired):
raise GMError(k + ' is required')


if product[k] == '' and k in fieldIsRequired:
raise GMError(k + ' is required')

--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Sep 1 '05 #2

P: n/a
jdonnell wrote:
I want to check if a value is in an array. I'm currently doing it as
follows, but I just don't like this way of doing it. It seems
unpythonic.

fieldIsRequired = true
try:
notRequiredAry.index(k)
fieldIsRequired = false
except ValueError:
pass


fieldIsRequired = not (k in notRequiredAry)

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Sep 1 '05 #3

P: n/a
jdonnell wrote:
I want to check if a value is in an array. I'm currently doing it as
follows, but I just don't like this way of doing it. It seems
unpythonic.

fieldIsRequired = true
try:
notRequiredAry.index(k)
fieldIsRequired = false
except ValueError:
pass

# throw expception if field is required and is empty
if(product[k] == '' and fieldIsRequired):
raise GMError(k + ' is required')


if product[k] == '' and k in notRequiredAry:
raise GMError(k + ' is required')
Sep 1 '05 #4

P: n/a
You can check for membership in a list at least these ways:

my_list.index(candidate)
-returns the index into my_list of the first occurrence of candidate.
Raises ValueError if candidate doesn't occur in my_list.

my_list.find(candidate)
-returns the index into my_list of the first occurrence of candidate.
Returns -1 if candidate doesn't occur in my_list.

candidate in my_list
-returns True if candidate occurs in my_list
The last is nicest (most pythonic?) if you don't actually need the
index, which it seems you don't. That being said, you should consider
the, what I believe is called, Complexity of your algorithm. In
particular, you should recall that checking for membership in a list
takes an amount of time proportional to the length of the list, while
checking for membership in a set (or dictionary) takes a constant
amount of time regardless of the size of the set. From what I see, it
appears that you don't necessarily need to keep the notRequiredAry
things in order, but that you really just need to kknow the set of
things that are not required. If you are making this check a lot, for
instance looping over a big list and checking for each member whether
it is not required, you should consider using a set instead of a list.
It might have the benefit of making your code more readable too.

Sep 1 '05 #5

P: n/a
Thanks for all the help everyone.

Steve, sets are perfect. I didn't even realize they existed. Somehow I
completely missed that part of the tutorial. Thanks :)

Sep 1 '05 #6

P: n/a
"Steve M" <sj******@gmail.com> writes:
You can check for membership in a list at least these ways:
Two ways - idnex and the in keyword.
my_list.find(candidate)
-returns the index into my_list of the first occurrence of candidate.
Returns -1 if candidate doesn't occur in my_list.


Lists don't have a find method. strings do. Why is a good question.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Sep 2 '05 #7

P: n/a
Mike Meyer wrote:
"Steve M" <sj******@gmail.com> writes:
my_list.find(candidate)
-returns the index into my_list of the first occurrence of candidate.
Returns -1 if candidate doesn't occur in my_list.


Lists don't have a find method. strings do. Why is a good question.


Probably because at one point lists didn't even have the index() method,
and when it was suggested and (I believe) Raymond H. added it, I suspect
nobody also suggested that implementing find() was a good idea so it
wasn't done.

And, given both that index() exists and the recent discussion about the
potential problems caused by find() returning a valid slice index when
it fails, maybe it's _not_ a good idea...

-Peter
Sep 2 '05 #8

P: n/a
Peter Hansen wrote:
Probably because at one point lists didn't even have the index() method,
and when it was suggested and (I believe) Raymond H. added it


the list "index" method has been in Python since, like, forever (early 1991,
according to CVS, which means that it was added about two years before
the first 1.0 alpha was made public...)

</F>

Sep 2 '05 #9

P: n/a
Fredrik Lundh wrote:
Peter Hansen wrote:
Probably because at one point lists didn't even have the index() method,
and when it was suggested and (I believe) Raymond H. added it


the list "index" method has been in Python since, like, forever (early 1991,
according to CVS, which means that it was added about two years before
the first 1.0 alpha was made public...)


D'oh! My apologies: I was misremembering the addition of the optional
"start" and "stop" arguments to the existing list() method, which
happened in June 2003 (SF #754014 at
http://sourceforge.net/tracker/index...70&atid=355470
).

-Peter
Sep 2 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.