For loop / Natual Language Processing

Hello guys!
I have a list of words in a variable called stems. I need to write a for loop which will print 5 words in every window. (i.e. The window width is 5 words)

stems: List = [
    and more...

Output should be:
0 [‘stem1’, ‘stem2’, ‘stem3’, ‘stem4’, ‘stem5’]
1 [‘stem6’, ‘stem7’, ‘stem8’, ‘stem9’, ‘stem10’]
2 [‘stem11’, ‘stem12’, ‘stem13’, ‘stem14’, ‘stem15’]
3 [‘stem16’, ‘stem17’, ‘stem18’, ‘stem19’, ‘stem20’]

The code that I have now (doesn’t work properly)

n = 100
for i in range(len(stems)-n):
    print(i, stems[i:5])

stems[i:5] goes from whatever i is to number5. I wonder if you meant stems[i:i+5]. You still need to get your i right, but that will help the print.

range also takes a step operator. You might consider making range start at 0 explicitly, then you can add a step value of 5 to it.

1 Like
for start in range(0, len(stems), 5):
    print(start, stems[start:start+5])
1 Like

Thank you!!!