The original code was somehow complex, i simplify it as:
Given:
- list of class instance, e.g.: l=[c1,c2,c3, …]
- each instance has a member variable list, e.g. c1.memList=[3,2,5], c2.memList=[1,2]
Todo:
select those instances in l, whose memList has only ‘3’-modulo item , e.g, c3.memList=[3,6,9,3,27]
I thought to code it like this:
newl = [ n for n in l if len( [m for m in n.memList if m%3] )==0 ]
But: list comprehension does not allow this by saying ‘m is not defined’
Question: how to code this in a pythonic way?
New edit: Sorry I made a typo (mistyped if to in), it worked. I will propose to close this question.
I did not get any Error concerning ‘m is not defined’ the reasons must be outside of this snippet.
I would recommend something like this, the all() function improves readability. It is allways good to use the list Syntax cause it speeds calculations.