Python Forum
looping through lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: looping through lists (/thread-7444.html)



looping through lists - brianl - Jan-10-2018

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,


RE: looping through lists - buran - Jan-10-2018

& 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)]



RE: looping through lists - brianl - Jan-10-2018

Great! Thanks! it helped a lot.
I have not reached that section of the tutorial explaining logical And, etc. yet! I was just experimenting!