9

How can I implement this kind of iteration similar to sliding window method in python.

Given s = [1, 2, 3, 4, 5, 6]

[1, 2, 3]
   [2, 3, 4]
      [3, 4, 5]    
         [4, 5, 6]
            [5, 6]
               [6]
DevEx
  • 4,337
  • 13
  • 46
  • 68

1 Answers1

9
l = [1, 2, 3, 4, 5, 6]    
for i in range(len(l)):
    print l[i : i+3]

Output

[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
[5, 6]
[6]
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218