Change language

Python | Boolean list of AND and OR operations

Method # 1: And operation — Using `all()`
The solution to this problem is quite simple, but knowledge of the application is required. The all () function performs a logical AND on the list and returns the result.

 ` # Python3 code to demonstrate how it works ` ` # A logical list of AND and OR operations ` ` # AND Operation - Use all () ` ` `  ` # initialize the list ` ` test_list ` ` = ` ` [` ` True ` `, ` ` True ` `, ` ` False ` `, ` ` True ` `, ` ` False ` `] `   ` # print about original list ` ` print ` ` (` ` "The original list is: "` ` + ` ` str ` ` (test_list)) `   ` # Logical list of AND and OR operations ` ` # AND Operation - Use all () ` ` res ` ` = ` ` all ` ` (test_list) `   ` # print result ` ` print ` ` (` ` "Result after performing AND among elements:" ` ` + ` ` str ` ` (res)) `

Output:

The original list is: [True, True, False, True, False] Result after performing AND among elements: False

Method # 2: OR operation — using `any()`
This task can be accomplished with ` any () `. This checks any element True in the list and returns True, in which case it returns False.

 ` # Python3 code to demonstrate how it works ` ` # A logical list of AND and OR operations ` ` #OR operation - Using any () `   ` # initialize the list ` ` test_list ` ` = ` ` [` ` True ` `, ` ` True ` `, ` ` False ` ` , ` ` True ` `, ` ` False ` `] ` ` `  ` # print original list ` ` print ` ` (` `" The original list is: "` ` + ` ` str ` ` (test_list )) `   ` # Boolean list of AND and OR operations ` ` Operation # OR - Using any () ` ` res ` ` = ` ` any ` ` (test_list) `   ` # print result ` ` print ` ` (` ` "Result after performing OR among elements:" ` ` + ` ` str ` ` (res)) `

Exit:

` The original list is: [True, True, False, True, False] Result after performing OR among elements: True `

Shop

Best laptop for Sims 4

\$

Best laptop for Zoom

\$499

Best laptop for Minecraft

\$590

Best laptop for engineering student

\$

Best laptop for development

\$

Best laptop for Cricut Maker

\$

Best laptop for hacking

\$890

Best laptop for Machine Learning

\$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method