0

What's the best way to iterate through a list of file names by group of six, while for every file name within that group of six an action should occur?

Of course the following code isn't working but might help you to understand what I want:

list_of_names = [a,b,c,d,e,f,g,h.....]


while i < 6:   # for a - f
    for fe in list_of_names:
        *do stuff*
    i=i+1

then repeat this for the next 6 elements starting from g - ....

I hope it's clear what I mean.

Shaun
  • 461
  • 3
  • 5
  • 22
  • 1
    Possible duplicate of [What is the most “pythonic” way to iterate over a list in chunks?](https://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks) and [Iterate an iterator by chunks (of n) in Python?](https://stackoverflow.com/questions/8991506/iterate-an-iterator-by-chunks-of-n-in-python) – pault Oct 30 '18 at 14:58
  • i believe you mean you want to iterate over the list by steps or chunks. maybe have a look at this https://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks – Dap Oct 30 '18 at 14:59

1 Answers1

0

You can use list slicing:

import string

# assume lowercase ascii are the file names
names = list(string.ascii_lowercase)

for i in range(0, len(names), 6):
    print(names[i:i+6])
yang5
  • 1,125
  • 11
  • 16
  • I tried your approach but now I have a different question here: https://stackoverflow.com/questions/53077225/python-debugging-loop-for-plotting-isnt-showing-the-next-plot Could you take a look at it? Thank you very much – Shaun Oct 31 '18 at 14:29