NEWBIE: Extending a For Statement.

M

mosscliffe

I keep seeing examples of statements where it seems conditionals are
appended to a for statement, but I do not understand them.

I would like to use one in the following scenario.

I have a dictionary of

mydict = { 1: 500, 2:700, 3: 800, 60: 456, 62: 543, 58: 6789}

for key in mydict:
if key in xrange (60,69) or key == 3:
print key,mydict[key]

I would like to have the 'if' statement as part of the 'for'
statement.

I realise it is purely cosmetic, but it would help me understand
python syntax a little better.

Thanks

Richard
 
D

Dustan

I keep seeing examples of statements where it seems conditionals are
appended to a for statement, but I do not understand them.
I would like to use one in the following scenario.
I have a dictionary of
mydict = { 1: 500, 2:700, 3: 800, 60: 456, 62: 543, 58: 6789}
for key in mydict:
if key in xrange (60,69) or key == 3:
print key,mydict[key]
I would like to have the 'if' statement as part of the 'for'
statement.
I realise it is purely cosmetic, but it would help me understand
python syntax a little better.

Only list comprehensions and generator expressions support this extension
to the loop syntax.

[key, mydict[key] for key in mydict if key in xrange(60, 69) or key == 3]
(key, mydict[key] for key in mydict if key in xrange(60, 69) or key == 3]
ack!
(key, mydict[key] for key in mydict if key in xrange(60, 69) or key == 3]
File "<stdin>", line 1
(key, mydict[key] for key in mydict if key in xrange(60, 69) or
key == 3]
^
SyntaxError: invalid syntax

Perhaps you meant that second one to be:
(key, mydict[key] for key in mydict if key in xrange(60, 69) or key ==
3)
For the statement form of 'for', there is no syntactic way to combine it
with 'if' into a single statement.

But there is a dumb hack to get it to happen, but I'm not going to it
here, because Guido never meant for generator expressions to be used
that way. To the OP: I would suggest you just live what might seem
like excess indentation; it's good for your eyes.
 
D

Dustan

I keep seeing examples of statements where it seems conditionals are
appended to a for statement, but I do not understand them.
I would like to use one in the following scenario.
I have a dictionary of
mydict = { 1: 500, 2:700, 3: 800, 60: 456, 62: 543, 58: 6789}
for key in mydict:
if key in xrange (60,69) or key == 3:
print key,mydict[key]
I would like to have the 'if' statement as part of the 'for'
statement.
I realise it is purely cosmetic, but it would help me understand
python syntax a little better.

Only list comprehensions and generator expressions support this extension
to the loop syntax.

[key, mydict[key] for key in mydict if key in xrange(60, 69) or key == 3]
(key, mydict[key] for key in mydict if key in xrange(60, 69) or key == 3]
ack!
(key, mydict[key] for key in mydict if key in xrange(60, 69) or key == 3]
File "<stdin>", line 1
(key, mydict[key] for key in mydict if key in xrange(60, 69) or
key == 3]
^
SyntaxError: invalid syntax

Perhaps you meant that second one to be:
(key, mydict[key] for key in mydict if key in xrange(60, 69) or key ==
3)
For the statement form of 'for', there is no syntactic way to combine it
with 'if' into a single statement.

There is a dumb hack to get it to happen, but I'm not going to it
here, because Guido never meant for generator expressions to be used
that way. To the OP: I would suggest you just live what might seem
like excess indentation; it's good for your eyes.
 
L

Larry Bates

mosscliffe said:
I keep seeing examples of statements where it seems conditionals are
appended to a for statement, but I do not understand them.

I would like to use one in the following scenario.

I have a dictionary of

mydict = { 1: 500, 2:700, 3: 800, 60: 456, 62: 543, 58: 6789}

for key in mydict:
if key in xrange (60,69) or key == 3:
print key,mydict[key]

I would like to have the 'if' statement as part of the 'for'
statement.

I realise it is purely cosmetic, but it would help me understand
python syntax a little better.

Thanks

Richard

I find something like the following easy to read and easy to
extend the contents of searchkeys in the future.

searchkeys=range(60, 69) + [3]
goodlist=[(k, v) for k, v in mydict.items() if k in searchkeys]
for key, value in goodlist:
print k,v

-Larry
 
B

bearophileHUGS

mosscliffe:
if key in xrange (60,69) or key == 3:

I keep seeing again and again code like this, mostly from people not
much expert of Python, but the PEP 260 shows the fast in was removed,
so it's O(n). Maybe removing the fast __contains__ was bad for
necomers (or just the casual Python users, that I belive is really
large).

Bye,
bearophile
 
M

mosscliffe

mosscliffe said:
I keep seeing examples of statements where it seems conditionals are
appended to a for statement, but I do not understand them.
I would like to use one in the following scenario.
I have a dictionary of
mydict = { 1: 500, 2:700, 3: 800, 60: 456, 62: 543, 58: 6789}
for key in mydict:
if key in xrange (60,69) or key == 3:
print key,mydict[key]
I would like to have the 'if' statement as part of the 'for'
statement.
I realise it is purely cosmetic, but it would help me understand
python syntax a little better.
Thank you all - it is all helping to expand my python knowledge.

I appreciate your comment about sticking with the simple format.

I am finding 'python', one of the most straightforward languages to
learn, yet very powerful in its capabilities. The whole list concept,
is so akin to programming tasks, that it means I can think more about
design, than worrying about how to store and access data.

Richard

I find something like the following easy to read and easy to
extend the contents of searchkeys in the future.

searchkeys=range(60, 69) + [3]
goodlist=[(k, v) for k, v in mydict.items() if k in searchkeys]
for key, value in goodlist:
print k,v

-Larry
 
M

mosscliffe

mosscliffe:


I keep seeing again and again code like this, mostly from people not
much expert of Python, but the PEP 260 shows the fast in was removed,
so it's O(n). Maybe removing the fast __contains__ was bad for
necomers (or just the casual Python users, that I belive is really
large).

Bye,
bearophile

Being a non-expert in python in fact just a beginner / casual user,
can you expand on how 0(n) relates to
if key in xrange (60,69) or key == 3:

My mind tends to go blank at the mention of __xxx__.

R
 
W

Wildemar Wildenburger

Dustan said:
(key, mydict[key] for key in mydict if key in xrange(60, 69) or key == 3]
File "<stdin>", line 1
(key, mydict[key] for key in mydict if key in xrange(60, 69) or
key == 3]
^
SyntaxError: invalid syntax

Perhaps you meant that second one to be:
(key, mydict[key] for key in mydict if key in xrange(60, 69) or key ==
3)
Clearly not! Its called *list*-comprehension, not tuple-comprehension. ;)
This works: [(key, mydict[key]) for key in mydict if key in xrange(60,
69) or key ==3]
(Note the parenthesis around (key, mydict[key]), they are MENDATORY.)

regards
W
 
D

Dustan

Being a non-expert in python in fact just a beginner / casual user,
can you expand on how 0(n) relates to
if key in xrange (60,69) or key == 3:

the "key in xrange(60,69)" part: all that does is iterate through
xrange(60, 69) and when a match is met, it returns true. If it ends up
iterating through the whole list without finding a match, it returns
false.
My mind tends to go blank at the mention of __xxx__.

The above is the default behavior of the 'in' operator. That default
behavior can be overridden by a class that has the __contains__
method. So "XXX in YYY" expands roughly to the following (as I
understand it; I haven't actually looked it up):

if hasattr(YYY, "__contains__"):
return YYY.__contains__(XXX)
else:
for i in YYY:
if XXX == YYY:
return True
return False
 
S

Sion Arrowsmith

mosscliffe:
I keep seeing again and again code like this, mostly from people not
much expert of Python, but the PEP 260 shows the fast in was removed,
so it's O(n).

If you're going to point that out, you should at least also mention
the "correct" formulation:

if 60 <= key < 69 or key == 3:

And if anyone cares about performance:
$ python -mtimeit -s 'x = 67' 'x in range(60, 69)'
1000000 loops, best of 3: 0.906 usec per loop
$ python -mtimeit -s 'x = 67' 'x in xrange(60, 69)'
1000000 loops, best of 3: 0.744 usec per loop
$ python -mtimeit -s 'x = 61' 'x in range(60, 69)'
1000000 loops, best of 3: 0.707 usec per loop
$ python -mtimeit -s 'x = 61' 'x in xrange(60, 69)'
1000000 loops, best of 3: 0.471 usec per loop
$ python -mtimeit -s 'x = 67' '60 <= x < 69'
10000000 loops, best of 3: 0.155 usec per loop
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top