Search code examples
loopsnestedlist-comprehensionnested-loops

double variable nest for loop in comprehension list


I'm trying to improve my code by implementing comprehension list.

I have de following statement.

test = [expression.match(self.sourceModel().index(source_row, column, source_parent).data())
                for expression in liste for liste, column in self._filters.items()]

the test is a regex matching between strings, from a list of expressions, list which is contained into a dict, having the column as a key, and list of expressions as a value

I can't get why my variable "liste" is marked as unreferenced, since it is mentionned in the last for loop, any ideas ?


Solution

  • Code

    test = [expression.match(self.sourceModel().index(source_row, column, source_parent).data()) for expression in liste for liste, column in self._filters.items() for expression in liste ]
    

    Explaination

    your liste variable was unreferenced because you referred the liste variable in a loop that was executed only after you used the var itself !!

    remember that is right to think to list comprehension liek a reversed structure, bfore the item definition and then the loop definition, but in every section the logical order is the same of the normal loop code