[Tutor] help with slice

Alan Gauld alan.gauld at btinternet.com
Wed Mar 5 01:29:31 CET 2008

"Ole Henning Jensen" <tjampman at gmail.com> wrote

> What you need to do is loop all the way through your list and then 
> only
> do something to every third element in the list

That's not quite what the OP asked for, he wanted the third
element from every sublist in the master list

> This is an example of a for loop, that prints out every 3 element:

You can do this using slicing, no need for a for loop

L = range(10)
print L{2::3]

the 2 is the start position - the 3rd element, then the
final 3 is the step size, giving every third element up
to the end of the list.

Interesting maybe, but not what the OP asked for.

Alan Gauld
Author of the Learn to Program web site
Temorarily at:

More information about the Tutor mailing list