Python Forum
effective means to flip boolean values?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
effective means to flip boolean values?
#1
Is there a method or something to easily flip a Boolean value from False to True or vice versa?
Reply
#2
You use not to do that, obviously.
Reply
#3
ndc85430 idea expressed in code:

>>> spam = True
>>> spam = not spam
>>> spam
False
>>> spam = not spam
>>> spam
True
Another possibility is to use itertools.cycle(). This is useful, if there are more than two values to flip:

>>> from itertools import cycle
>>> switch = cycle([True, False]).__next__
>>> spam = switch()
>>> spam
True
>>> spam = switch()
>>> spam
False
>>> spam = switch()
>>> spam
True
>>> switch = cycle(['ON', 'STANDBY', 'OFF']).__next__
>>> spam = switch()
>>> spam
'ON'
>>> spam = switch()
>>> spam
'STANDBY'
>>> spam = switch()
>>> spam
'OFF'
>>> spam = switch()
>>> spam
'ON'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(Aug-24-2019, 08:16 PM)perfringo Wrote: ndc85430 idea expressed in code:

>>> spam = True
>>> spam = not spam
>>> spam
False
>>> spam = not spam
>>> spam
True
Another possibility is to use itertools.cycle(). This is useful, if there are more than two values to flip:

>>> from itertools import cycle
>>> switch = cycle([True, False]).__next__
>>> spam = switch()
>>> spam
True
>>> spam = switch()
>>> spam
False
>>> spam = switch()
>>> spam
True
>>> switch = cycle(['ON', 'STANDBY', 'OFF']).__next__
>>> spam = switch()
>>> spam
'ON'
>>> spam = switch()
>>> spam
'STANDBY'
>>> spam = switch()
>>> spam
'OFF'
>>> spam = switch()
>>> spam
'ON'


Thank you! That looks super useful!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,102 Jan-09-2022, 02:39 AM
Last Post: Python84
  Understand what it means that everything in Python is an object... bytecrunch 8 3,783 Mar-19-2021, 04:47 PM
Last Post: nilamo
  Python - Most effective way to correct keyboard-user-input. ppel123 8 4,150 Apr-08-2020, 07:41 AM
Last Post: ppel123
  what "return" means? PY_beginner 9 3,895 Oct-10-2019, 07:18 AM
Last Post: newbieAuggie2019
  Just some Boolean values musings newbieAuggie2019 2 1,874 Oct-04-2019, 06:15 AM
Last Post: newbieAuggie2019
  what means in Python the "->"? lsepolis123 2 1,998 Aug-22-2019, 08:08 AM
Last Post: DeaD_EyE
  Smtplib: What does context argument means? Pythenx 1 3,077 Mar-27-2019, 06:25 PM
Last Post: nilamo
  what from .something means sylas 1 2,588 May-17-2018, 06:19 AM
Last Post: buran
  [split] Coin Flip Program Crackity 5 4,805 Sep-25-2017, 03:48 AM
Last Post: Crackity
  Coin Flip Program Warbit 6 8,088 Mar-27-2017, 11:33 PM
Last Post: Warbit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020