sebastian sebastian - 1 month ago 5
Python Question

If all 9 strings in list is replaced: do something

As the title:
If all 9 strings in list is replaced, then a task should run.

Here is the code:

list = [
['0', '0', '0'],
['0', '0', '0'],
['0', '0', '0']
]


If all of them is either replaced by a "1" or "2" doesnt matter which is replaced by what. Then it should run a task.

So how do I if all of the 9 spots have been replaced by either a "1" or "2"?

There is simply too many possibilities to write all the combinations down, and compare them to the list.

Answer

How about this,

s = set([item for sublist in lists for item in sublist])  # flat a list of lists into a set

if '0' not in s:
    do_something()