Python list iteration

I have a list iteration in python, defined like this:

for i in range(5):
    for j in range(5):
        if i != j:
            print i , j


So, for every element in my specific range [0..5], I want to get every i element, but also all other elements that are not i.

This code works exactly as I expect, but is there a cleaner way to do this?


source to share

2 answers

Use itertools.permutations


import itertools as it
for i, j in it.permutations(range(5), 2):
    print i, j




[(x, y) for x in range (5) for y in range (5) if x! = y]



All Articles