Is there a better way to chose a slice of a list?

John Yeung gallium.arsenide at gmail.com
Fri May 8 19:55:39 EDT 2009


On May 8, 3:03 pm, walterbyrd <walterb... at iname.com> wrote:
> This works, but it seems like there should be a better way.
>
> --------------
> week = ['sun','mon','tue','wed','thu','fri','sat']
> for day in week[week.index('tue'):week.index('fri')]:
>    print day
> ---------------

I think you should provide much more information, primarily why you
want to do this.  What is the larger goal you are trying to achieve?

In the absence of further information, it seems to me that you are
trying to create an enumerated type.  For various ideas to achieve
this simply, depending on your purpose, see

  http://norvig.com/python-iaq.html

If you want a more thorough treatment, maybe try this package:

  http://pypi.python.org/pypi/enum/

There may be other recipes and packages; you can Google for them using
keywords "python enum" or similar.

John



More information about the Python-list mailing list