Hmm, sounds like homework, but I'll bite.
The underlying problem does sound like homework, but the OP posted a working solution, and was only looking for ways to improve it. So I'm a little more lenient on providing alternatives. It's true that the homework problem may have been exactly as posted ("Improve this code"), but that's less likely in my estimation.
-tkc -- http://mail.python.org/mailman/listinfo/python-list