1

In Python I have a series of nested for loops in this format:

for i in range(2):
    for j in range(2):
      for k in range(2):

and so on, all with the same range. Is there an easy way to simplify this into something like: for i, j, k in range(2)? I have been looking for a way to condense it down to one line and haven't found one.

Celeo
  • 5,583
  • 8
  • 39
  • 41
  • http://stackoverflow.com/q/18369260/3001761 – jonrsharpe Sep 16 '14 at 17:38
  • 1
    or [Avoiding nested for loops](http://stackoverflow.com/q/11174745) – Martijn Pieters Sep 16 '14 at 17:46
  • This isn't necessarily a dupe as it can be solved in different ways based on what the intention is. If OP wants to select different 3-length combinations (with repeats) from a pool of 2 items, then they could iterate with "for i,j,k in itertools.combinations_with_replacement(range(2), 3)" – Jeremy Brown Sep 16 '14 at 17:50
  • 1
    Aaaaand I just realized that I'm wrong and combinations_with_replacement didn't give what I expected. itertools.product(range(2), repeat=3) would be correct. – Jeremy Brown Sep 16 '14 at 19:11

0 Answers0