Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
and/or in Lists
#1
Hello everyone!

Please advise why it does not works

test=['a', 'b', 'c', 'd', 'h']
if 'a' and 'b' in test:
    print ('everything is ok')
else:
    print ('something went wrong')
Result is "everything is ok"

But when i use Or statement:

test2=['a', 'b', 'c', 'd', 'h']
if 'i' or 'j' in test2:
    print ('everything is ok')
else:
    print ('something went wrong')
Result is "everything is ok" as well.
Reply
#2
https://python-forum.io/Thread-Multiple-...or-keyword

test=['a', 'b', 'c', 'd', 'h']
if 'a' in test and 'b' in test:
    print ('everything is ok')
else:
    print ('something went wrong')
same for or

you may want to look also at all() and any() built-in functions
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
It does not work like english, You need to check each condition specifically.

if ('a' in test) and ('b' in test)
Reply
#4
You were shown the proper form; let me provide you with an explanation.

In Python any non-empty value evaluates to True in boolean expressions, and empty - empty list, empty dict, empty string, etc - to False. Also, 0 and None are False equivalent, and non-zero value is True

But there's an interesting Python peculiarity - a composite boolean expression takes the value of the last expression that defines whether the final result is True or False

Output:
In [10]: test=['a', 'b', 'c', 'd', 'h'] In [11]: 'a' and 'b' in test Out[11]: True In [12]: 'i' or 'j' in test Out[12]: 'i' In [13]: 'b' in test and 'a' Out[13]: 'a' In [14]: 'a' and True Out[14]: True In [15]: True and 'a' Out[15]: 'a' In [16]: 'a' and 0 Out[16]: 0 In [17]: False and 'a' Out[17]: False In [18]: 'a' or False Out[18]: 'a' In [19]: True or 'a' Out[19]: True In [20]: '' and False Out[20]: ''
Occasionally this is used as a replacement for ternary operation
Output:
In [21]: x = 1 In [22]: 'a' if x == 2 else 'b' Out[22]: 'b' In [23]: x == 2 and 'a' or 'b' Out[23]: 'b'
There are couple of shortcomings for the latter technique
  • You have to be very careful about the order of operands
  • Ternary operations are faster
  • You may be chased by a mob of purists with pitchforks and torches Naughty
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,586 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,536 Mar-20-2019, 08:01 PM
Last Post: stillsen

Forum Jump:

User Panel Messages

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