python - Two nested for loops -


i dont know how ask question here trying do.

lists = [] x in range(3):   y in range(3):     if x!=y:       lists.append([x,y]) 

is there simple solution doesnt give me lists same reversed: example [2,0] , [0,2]?

i know go through lists , remove them afterwards there solution not make list? (sorry english isnt perfect)

you can use itertools.combinations

>>> itertools import combinations >>> list(combinations(range(3), 2)) [(0, 1), (0, 2), (1, 2)] 

with above example take combination of 2 elements range(3) without repeating elements.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -