Testing if a value is in either one of two lists
Testing if a value is in either one of two lists
I have one area in my code that checks if item of one list is in other two lists and return a result depending on that.
Apparently, first IF clause is always true and only result from that clause is returned.
Here is the example:
from datetime import date
days = [date(2018, 9, 10), date(2018, 9, 11), date(2018, 9, 12)]
list_one = [date(2018, 9, 13), date(2018, 9, 14), date(2018, 9, 15)]
list_two = [date(2018, 9, 8), date(2018, 9, 9), date(2018, 9, 10)]
for day in days:
if day not in(list_one, list_two):
print('Case one')
elif day in list_one:
print('Case two')
elif day in list_two:
print('Case three')
day
(list_one, list_two)
day
list
What did you expect
if day not in (list_one, list_two)
to mean? Is it "if day
is in list_one
or list_two
" or "if day
is in list_one
and list_two
"?– Aran-Fey
Sep 10 '18 at 11:08
if day not in (list_one, list_two)
day
list_one
list_two
day
list_one
list_two
You can only check if a whole list is in a tuple of lists, such as
list_one in (list_one, list_two)
, list_two in (list_one, list_two)
, [date(2018, 9, 13), date(2018, 9, 14), date(2018, 9, 15)] in (list_one, list_two)
, etc.– Martijn Pieters♦
Sep 10 '18 at 11:08
list_one in (list_one, list_two)
list_two in (list_one, list_two)
[date(2018, 9, 13), date(2018, 9, 14), date(2018, 9, 15)] in (list_one, list_two)
Your first if compares a single day against each list of days, not against the days in the lists. And a day can never be a list of days
– Michael Butscher
Sep 10 '18 at 11:09
Srđan, if your question has been answered please consider accepting one of the answers by clicking the green checkmark next to it. Thanks!
– timgeb
Sep 11 '18 at 10:55
5 Answers
5
(list_one, list_two)
is a tuple of exactly two elements, containing list_one
and list_two
. Since a day
is never equal to a list, day not in (list_one, list_two)
turns out to be True.
(list_one, list_two)
list_one
list_two
day
day not in (list_one, list_two)
You could either merge the lists and write
lists = list_one + list_two
if day not in lists:
...
or use
if day not in list_one and day not in list_two:
...
or alternatively, applying De Morgan's laws:
if not (day in list_one or day in list_two):
...
to express that day
is in neither of those lists.
day
I'd also recommend using
set
and set.union
, as there are repeated lookups here.– jpp
Sep 10 '18 at 11:10
set
set.union
@jpp using sets is often a good optimization when the items are hashable, but I'll stick with the lists here because explaining to OP why the code does not work and how to fix it with minimal changes seems more important than the optimizaton that looks quite different.
– timgeb
Sep 10 '18 at 11:13
Sure, to be clear the items here are hashable. Sometimes it's worth a reminder that underlying a
datetime
object is an integer.– jpp
Sep 10 '18 at 11:14
datetime
Change the first if
to
if
if day not in list_one + list_two
Currently you don't have a list of elements, you have a tuple of two lists. So, to be in
it, the element has to be one of those lists.
in
from datetime import date
days = [date(2018, 9, 10), date(2018, 9, 11), date(2018, 9, 12)]
list_one = [date(2018, 9, 13), date(2018, 9, 14), date(2018, 9, 15)]
list_two = [date(2018, 9, 8), date(2018, 9, 9), date(2018, 9, 10)]
for day in days:
if (day not in list_one and day not in list_two):
print('Case one')
elif day in list_one:
print('Case two')
elif day in list_two:
print('Case three')
Since you already have two if
blocks testing if day
is in either lists, for your purpose it's a easier (and more efficient) to simply use the else
block for the case where day
is in neither lists:
if
day
else
day
if day in list_one:
print('Case two')
elif day in list_two:
print('Case three')
else:
print('Case one')
Or any
, to check if any elements are true, iterate trough the two list [list_one,list_two]
if one of them are it will be true because using in
will declare a Boolean statement:
any
[list_one,list_two]
in
...
if any(day in i for i in [list_one,list_two]):
...
...
While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Sep 11 '18 at 7:04
@hellow Okay I'll edit
– U9-Forward
Sep 11 '18 at 7:39
@hellow Edited mine
– U9-Forward
Sep 11 '18 at 7:41
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
you're checking if
day
is (not) in the tuple(list_one, list_two)
, when I suspect you wish to check whetherday
is in either of thelist
s.– Zinki
Sep 10 '18 at 11:06