is there a better way to check an array?

J

jdonnell

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')
 
R

Robert Kern

jdonnell said:
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
(e-mail address removed)

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

Benjamin Niemann

jdonnell said:
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')
 
W

Wojciech Mula

jdonnell said:
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')
 
S

Steve M

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.
 
J

jdonnell

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

Mike Meyer

Steve M said:
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
 
P

Peter Hansen

Mike said:
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
 
F

Fredrik Lundh

Peter said:
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>
 
P

Peter Hansen

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top