Transform the lists using a list comprehension

Here’s the Python list I’m working on:

nn,mm,pp = [1 if x =='0' else -1 for x in [['0','0'],['1','0'],['1','1']]]

My goal is to make nn = [1,1], mm = [-1,1] and pp = [-1,-1]. I can do that using for loop but it looks really long. I wonder is there a way I can do that using list comprehension? Thanks!

Answer

You are dealing with nested lists, which means for this particular problem you need to use nested loops:

>>> nested = [['0', '0'], ['1', '0'], ['1', '1']]
>>> [[1 if value == "0" else -1 for value in sublist] for sublist in nested]
[[1, 1], [-1, 1], [-1, -1]]