# How to Loop Through a Python List in Pairs, Sliding Windows, and Batches?

Table of Contents

## Method 1: Iterating over Consecutive (Sliding) Windows

Given are:

• Python list `lst`
• Window size `n`

Problem Formulation: How to loop through the list in consecutive element-windows of size `n`, so that in each iteration, you can access the `n` next elements in the list?

```# INPUT:
lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
n = 3 # window

# OUTPUT:
Window:  ['a', 'b', 'c']
Window:  ['b', 'c', 'd']
Window:  ['c', 'd', 'e']
Window:  ['d', 'e', 'f']
Window:  ['e', 'f', 'g']```

💡 Solution: To iterate over a Python list `lst` in windows of size `n`, iterate over all list indices `i` from 0 to the index of the `n`-th last list element (included). In the loop body, use the indices to retrieve the windows by slicing `lst[i:i+n]`. Slicing is memory-efficient because it doesn’t create copies of the original list.

Here’s the full code:

```lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
n = 3

for i in range(len(lst)-n+1):
batch = lst[i:i+n]
print('Window: ', batch)```

The output is:

```'''
OUTPUT:
Window:  ['a', 'b', 'c']
Window:  ['b', 'c', 'd']
Window:  ['c', 'd', 'e']
Window:  ['d', 'e', 'f']
Window:  ['e', 'f', 'g']
'''```

A variant of this is to iterate over consecutive windows of size 2: pairs!

## Method 2: Itearing over a List in Pairs

Problem Formulation: How to iterate over a list in pairs of elements?

This is a variant of the previous problem where the window size is `n=2`. We use the same approach:

💡 Solution: To iterate over a Python list `lst` in pairs, iterate over all list indices `i` from 0 to the index of the second last list element (included). In the loop body, use the indices to retrieve the windows by slicing `lst[i:i+2]`. Slicing is memory-efficient because it doesn’t create copies of the original list.

Here’s the full code:

```lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g']

for i in range(len(lst)-1):
pair = lst[i:i+2]
print('Pair: ', pair)```

The output is:

```'''
OUTPUT:
Pair:  ['a', 'b']
Pair:  ['b', 'c']
Pair:  ['c', 'd']
Pair:  ['d', 'e']
Pair:  ['e', 'f']
Pair:  ['f', 'g']
'''```

## Method 3: Slicing Over Non-Overlapping Windows (Batches)

A variant of the general problem addressed in Method 1 is to iterate over the list in batches of size `n`. However, the sliding windows are not allowed to overlap!

Problem Formulation: How to iterate over the list in batches of size `n` whereas each batch must have unique elements (non-overlapping batches)?

💡 Solution: You can do this by modifying the `range()` function that defines the loop variable `i` to also include a step size of `n`. You then use slicing `lst[i:i+n]` to access the `n` next elements from each index `i`. This results in non-overlapping windows (batches) of list slices.

```lst = ['a', 'b', 'c', 'd', 'e', 'f']
n = 3 # batch size

for i in range(0, len(lst)-n+1, n):
batch = lst[i:i+n]
print('Batch: ', batch)```

Here’s the output:

```'''
OUTPUT:
Batch:  ['a', 'b', 'c']
Batch:  ['d', 'e', 'f']
'''```

## Background Slicing

🎓 Background: Slicing is a concept to carve out a substring from a given string. Use slicing notation `s[start:stop:step]` to access every `step`-th element starting from index `start` (included) and ending in index `stop` (excluded). All three arguments are optional, so you can skip them to use the default values (`start=0`, `stop=len(lst)`, `step=1`). For example, the expression `s[2:4]` from string `'hello'` carves out the slice `'ll'` and the expression `s[:3:2]` carves out the slice `'hl'`.

## Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now! 