In some part of my Python program I have a val variable that can be 1 or 0. If it's 1 I must change to 0, if it's 0 I must change to 1.
How do you do it in a Pythonic way?
if val == 1:
val = 0
elif val == 0:
val = 1
it's too long!
I did:
swap = {0: 1, 1:0}
So I can use it:
swap[val]
Other ideas?
True
/False
instead of1
/0
in your case? – Coumasswap
solution because when I come back to this in years, it will immediately make sense to me. Also, it is extensible and customizable, in case I eventually need to work with more/different numbers. – Paginal