[Tutor] Poor style to use list as "array"?

Kent Johnson kent37 at tds.net
Mon Jul 6 00:49:32 CEST 2009


On Sun, Jul 5, 2009 at 2:48 PM, Angus Rodgers<angusr at bigfoot.com> wrote:

> for i in range(LEN - 1):
>    (count[i], amnt) = divmod(amnt, value[i])

How about this:
counts = []
for val in value:
  count, amnt = divmod(amnt, val)
  counts.append(count)

> This feels like a bit of a cheat, as if I am trying to program in
> Python as if it were some other more familiar language.  Should I
> have coded this differently?

Generally it's more straighforward to iterate over a list directly
rather than using an index.

> The full source code is here, in case anyone wants to look at it
> (but I'm not soliciting any more attention, as I've already been
> given quite a lot of it, and fear being offered the comfy chair!):

Don't worry, we have retired the comfy chair.

> Could I have used dictionaries instead, with the denomination names
> as keys?  Is it possible to guarantee a sequence in which the keys
> of a dictionary are iterated through?

In general no. Python 2.7 and 3.1 do have an ordered dictionary:
http://www.python.org/dev/peps/pep-0372/

Kent


More information about the Tutor mailing list