0

I have to check if numbers 1 to 9 are in a list/set

if 1 in factor_numbers and 2 in factor_numbers and 3 in factor_numbers and 4 in factor_numbers and 5 in factor_numbers and 6 in factor_numbers and 7 in factor_numbers and 8 in factor_numbers and 9 in factor_numbers and 0 in factor_numbers:

There has to be a more efficient way to do this check? The numbers do not necessarily appear adjacent to each other

Alec
  • 8,529
  • 8
  • 37
  • 63

1 Answers1

2

You can use the defined method set.issubset defined for sets, such that:

set([1, 2, 3, 4, 5, 6, 7, 8, 9]).issubset(set(factor_numbers))
Pierre
  • 6,084
  • 5
  • 32
  • 52
  • I may or may not have misunderstood your question. Can you provide a concrete example of what is not working as you want? – Pierre Mar 07 '19 at 03:55
  • I made a mistake, didn't include 0, and deleted my comment. This works perfectly. – Alec Mar 07 '19 at 03:56
  • Great, thanks for the clarification. – Pierre Mar 07 '19 at 03:57
  • What's the advantage of set.issubset over a simple <= or < operator? – Alec Mar 07 '19 at 23:40
  • The main advantage is of clarity. in the sense that looking at the code, it is obvious what `issubset` means. On the contrary, `<=` on a set probably means you have to go through the docs to understand what the code is doing. Under the hood, both are equivalent. – Pierre Mar 08 '19 at 14:24