Lists

Blake Winton bwinton at tor.dhs.org
Tue Apr 18 12:53:11 EDT 2000


On Tue, 18 Apr 2000 09:26:34 -0700, Daley, MarkX wrote:
>	a = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
>	for item in a:
>		try:
>			print b / a[item]
>		except ZeroDivisionError:
>			pass
>
>Why is the list being processed in reverse?

Hmmm... good question.
I don't think it is.  I think you're falling into a classic C trap.

Let's look at what the value of item and a[item] are in the loop.

               item     a[item]
Iteration 1     -5        1
Iteration 2     -4        2
Iteration 3     -3        3
...

The line you really wanted to write wasn't
  print b / a[item]
but instead was
  print b / item

Later,
Blake.
-- 
12:46pm up 20 days, 13:21, 1 user, load average: 1.00, 1.00, 1.00



More information about the Python-list mailing list