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

pruebauno at pruebauno at
Fri May 8 22:22:14 CEST 2009

On May 8, 3:03 pm, walterbyrd <walterb... at> 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
> ---------------

Depending on the context this style might help:

>>> week = ['sun','mon','tue','wed','thu','fri','sat']
>>> tue_fri = slice(week.index('tue'), week.index('fri'))
>>> for day in week[tue_fri]:
	print day

But I don't really see it as an improvement unless you are using those
intervals repeatedly.

More information about the Python-list mailing list