Swapping 1 with 0 and 0 with 1 in a Pythonic way

Python

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

This isn't pythonic, but it is language neutral. Often val = 1 - val is simplest.

Solution 2 - Python

The shortest approach is using the bitwise operator XOR.

If you want val to be reassigned:

val ^= 1

If you do not want val to be reassigned:

val ^ 1

Solution 3 - Python

Since True == 1 and False == 0 in python,

you could just use var = not var

It will just swap it.

Solution 4 - Python

Just another possibility:

i = (1,0)[i]

This works well as long as i is positive, as dbr pointed out in the comments it doesn't work for i < 0.

Are you sure you don't want to use False and True? It sounds almost like it.

Solution 5 - Python

In your case I recommend the ternary:

val = 0 if val else 1

If you had 2 variables to swap you could say:

(a, b) = (b, a)

Solution 6 - Python

To expand upon the answer by "YOU", use:

int(not(val))

Examples:

>>> val = 0
>>> int(not(val))
1

>>> val = 1
>>> int(not(val))
0

Note that this answer is only meant to be descriptive, not prescriptive.

Solution 7 - Python

Here's a simple way:

val = val + 1 - val * 2

For Example:

If val is 0

0+1-0*2=1

If val is 1

1+1-1*2=0

Solution 8 - Python

If you want to be short:

f = lambda val: 0 if val else 1

Then:

>>> f(0)
1
>>> f(1)
0

Solution 9 - Python

The most pythonic way would probably be

int(not val)

But a shorter way would be

-~-val

Solution 10 - Python

I have swapped 0s and 1s in a list.

Here's my list:

list1 = [1,0,0,1,0]

list1 = [i^1 for i in list1] 
#xor each element is the list
    
print(list1)

So the outcome is: [0,1,1,0,1]

Solution 11 - Python

Just another way:

val = ~val + 2

Solution 12 - Python

(0,1)[not val] flips the val from 0 to 1 and vice versa.

Solution 13 - Python

After seeing all these simpler answers i thought of adding an abstract one , it's Pythonic though :

val = set(range(0,2)).symmetric_difference(set(range(0 + val, val + 1))).pop()

All we do is return the difference of 2 sets namely [0, 1] and [val] where val is either 0 or 1.

we use symmetric_difference() to create the set [0, 1] - [val] and pop() to assign that value to variable val.

Solution 14 - Python

Another option:

val = (not val) * 1

Solution 15 - Python

Function with mutable argument. Calling the swaper() will return different value every time.

def swaper(x=[1]):
    x[0] ^= 1
    return x[0]

Solution 16 - Python

Your way works well!

What about:

val = abs(val - 1)

short and simple!

Solution 17 - Python

use np.where

ex.

np.where(np.array(val)==0,1,0)

this gives 1 where val is 0 and gives 0 where val is anything else, in your case 1

EDIT: val has to be array

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJuanjo ContiView Question on Stackoverflow
Solution 1 - PythonCB BaileyView Answer on Stackoverflow
Solution 2 - PythonenrnpfnfpView Answer on Stackoverflow
Solution 3 - PythonYOUView Answer on Stackoverflow
Solution 4 - PythonGeorg SchöllyView Answer on Stackoverflow
Solution 5 - PythonrplevyView Answer on Stackoverflow
Solution 6 - PythonAsclepiusView Answer on Stackoverflow
Solution 7 - PythonwizzView Answer on Stackoverflow
Solution 8 - PythonLoïc WolffView Answer on Stackoverflow
Solution 9 - Pythonnog642View Answer on Stackoverflow
Solution 10 - PythonRavi WrayView Answer on Stackoverflow
Solution 11 - PythonKristofView Answer on Stackoverflow
Solution 12 - Pythonrashmi.bmanyamView Answer on Stackoverflow
Solution 13 - PythonArnav DasView Answer on Stackoverflow
Solution 14 - PythongildniyView Answer on Stackoverflow
Solution 15 - PythonavalanchyView Answer on Stackoverflow
Solution 16 - PythonMatt RumbelView Answer on Stackoverflow
Solution 17 - Pythonha554anView Answer on Stackoverflow