Python Forum

Full Version: looping through lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have just started to learn Python and am going through tutorial. I have the following iteration:
[(x,y,z) for x in [1, 2, 3] for y in [1, 2, 3] for z in [1, 2, 3] if x != y & x != z & y != z]

The answer I get is:
[(1, 2, 3), (2, 1, 3), (3, 1, 2), (3, 2, 1)]

I was wondering why I should not get (2, 3, 1) and (1, 3, 2) as part of my answer.

Thanks,
& is bitwise and
it is not quite the same as boolean and

>>> [(x,y,z) for x in [1, 2, 3] for y in [1, 2, 3] for z in [1, 2, 3] if x != y & x != z & y != z]
[(1, 2, 3), (2, 1, 3), (3, 1, 2), (3, 2, 1)]
>>> [(x,y,z) for x in [1, 2, 3] for y in [1, 2, 3] for z in [1, 2, 3] if x != y and x != z and y != z]
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
Great! Thanks! it helped a lot.
I have not reached that section of the tutorial explaining logical And, etc. yet! I was just experimenting!