House3272
House3272

Reputation: 1037

Evaluate boolean tuple in Python

I'm trying to get this to evaluate to false.

(False,)

It's currently equaled to true, because I think the tuple is not empty. So how might one extract or cast this to a boolean? Thanks~

Upvotes: 2

Views: 11546

Answers (1)

en_Knight
en_Knight

Reputation: 5381

Extract the element from the tuple is the most simple approach:

value = (False,)[0]

Python2 is more lenient, but in general it isn't good practice to treat a tuple as a single value for comparison purposes (Python3 explicetly bans it)

Instead, look at the

all

and

any

functions for this behavior. As always, the documentation is your friend:

https://docs.python.org/2/library/functions.html#all

Upvotes: 8

Related Questions