1

When I use int

a = [1, 2, 3]
b = [3, 1, 2]

print(all(a) in b)

the result is True.

But, when I use characters

a = ["a", "b", "c"]
b = ["c", "b", "a"]

print(all(a) in b)

the result is False

Why in this case result is False?

Red
  • 26,798
  • 7
  • 36
  • 58
Alex
  • 39
  • 2
  • 3
    `all(a) in b` is `all([1, 2, 3]) in b` which is `True in b` which is `1 in b` which is`True`. – Klaus D. Dec 29 '20 at 13:28
  • 1
    FWIW, the correct way to use `all` here would be `all(i in b for i in a)`, though that's very inefficient. – deceze Dec 29 '20 at 13:31
  • This is not a duplicate of the linked posts, I'm not sure why this is closed. – Red Dec 29 '20 at 13:45

1 Answers1

2

all(a) in both cases returns True, so you are basically running

print(True in [3, 1, 2])

and

print(True in ["c", "b", "a"])

True == 1 returns True in python, so since there is the value 1 inside the integer b list, True in b returns True for the integer b list.

And because True woundn't equal to any string, True in b returns False for the string b list

Red
  • 26,798
  • 7
  • 36
  • 58