Well, I can tell you what's wrong with the syntax of your conditional: you at least need to include len(o)
inside the brackets.
[len(o) for o in octate_n]
This structure, [expr for var in list]
, is called a list comprehension and it's a shorthand for "evaluate expr
for each element of list
, with var
set to the element, and make a new list of the results." In your original code you didn't have any expr
which is why Python was complaining.
Now, even if you fix that, you wind up with
if [len(o) for o in octate_n] == 3:
which still has the problem that you are comparing a list to a number. While you can technically do that, it probably doesn't mean what you expect: a list will never be equal to a number, because they are different objects. It's not quite clear what you were trying to do, though. If you wanted to find whether all the elements of the list have length equal to 3, you would write
if all([len(o) == 3 for o in octate_n]):
or, more efficiently,
if all(len(o) == 3 for o in octate_n):
Note the absence of the square brackets in the later statement. This is because you don't actually need to make a list out of the results; you just need to be able to go through them one at a time and check if they're all true. Omitting the brackets is what indicates this to Python. (You can only do this inside parentheses.)