25

By mistake I have used more than one in keyword in an expression, but the code still works.

What is the meaning of:

"a" in "bar" in "foo"   # in ... ?

naively I thought that this was equivalent to ("a" in "bar") in "foo" or "a" in ("bar" in "foo") but it is not the case since both are not valid. I get the same behaviour in python2 or 3.

5
  • 2
    probably 'a' in 'bar' and 'bar' in 'foo' following a similar logic as x < y < z
    – Ma0
    Jun 29, 2017 at 6:38
  • 1
    @Ev.Kounis: why? I can't find it in the documentation. And what is the meaning of "a" in "b" in "c" in "'d" ... ? Jun 29, 2017 at 6:41
  • 1
    @RuggeroTurra By extension, that would be ("a" in "b") and ("b" in c") and ("c" in "d"). And "bar" in "a" in "aaa" does evaluate to False, so I'm not sure what your point is.
    – jamesdlin
    Jun 29, 2017 at 6:42
  • 1
    @RuggeroTurra because 'bar' in 'a' is False. My analogy was confusing. I meant that there are two separate checks; the result of the first does not partake in the second.
    – Ma0
    Jun 29, 2017 at 6:46
  • Duplicate: stackoverflow.com/q/38296689/2301450
    – vaultah
    Jun 29, 2017 at 18:15

3 Answers 3

41

in is considered a comparison operator, and from Python's documentation for them:

Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).

Formally, if a, b, c, ..., y, z are expressions and op1, op2, ..., opN are comparison operators, then a op1 b op2 c ... y opN z is equivalent to a op1 b and b op2 c and ... y opN z, except that each expression is evaluated at most once.

4

Python evaluates from left to right (you can control the flow / grouping with brackets, tho), and comparison operators can be chained arbitrarily so an expression:

"a" in "bar" in "foo" in "baz"

Essentially ends up as:

"a" in "bar" and "bar" in "foo" and "foo" in "baz"

Which resolves to False.

4
  • 8
    if it were just from left to right it would be (("a" in "bar") in "foo") in "baz" Jun 29, 2017 at 7:03
  • 5
    The evaluation order isn't the crux of the question.
    – jamesdlin
    Jun 29, 2017 at 9:56
  • @jamesdlin - it matters quite a bit with comparison operator chaining because evaluation breaks the moment it encounters a false comparison - "f" in "bar" in None is perfectly valid as the first (LTR order) comparison fails and Python breaks out, however "f" in "foo" in None will result in a TypeError.
    – zwer
    Jun 29, 2017 at 23:40
  • @zwer I didn't say it didn't matter. It's just not what the question is about.
    – jamesdlin
    Jun 29, 2017 at 23:59
1

This seems to mean the following:

("a" in "bar") and ("bar" in "foo") - or False

The following might help:

  • "a" in "bar" in "foo" => False
  • "a" in "bar" in "foobar" => True
  • "b" in "bar" in "foobar" => True
  • "c" in "bar" in "foobar" => False

I thought at first that it might have been "a" in ("bar" in "foo"), but that obviously would return the following:

TypeError: argument of type 'bool' is not iterable

Because ("bar" in "foo") returns False

Edit Fixed obvious typos

3
  • 3
    "b" in "bar" in "foo" => False. Should be False not True
    – hxysayhi
    Jun 29, 2017 at 6:56
  • 9
    Is there an extra in after the and in your first code line? Jun 29, 2017 at 9:16
  • Apologies, had a couple of typos in there - should make sense now :) Jun 29, 2017 at 23:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.