0

Python newbie here. I have four lists, three of which are nested lists and one that isn't. I'm searching for a way to zip the nested lists with the list such that the zip function compares each nested list item with the corresponding item in the main list.

main = [1,3]
a = [[1,2,3][4,5,6]]
b = [[0,1,2][3,4,5]]
c = [[2,3,4][5,6,7]]

>>>[[[True, False, False],[False,True,False],[False,False,False]],
[[False,False,False],[True,False,False],[False,False,False]]]

I tried something like this:

abc = zip(a,b,c)
test = (x==y for x, y in zip(main,*abc)

but I'm getting the error message "too many values to unpack". Any suggestions?

stdmn
  • 63
  • 2
  • 8

1 Answers1

2

The idea is to zip() the main list with the already zipped a, b and c lists and make a nested list comprehension:

>>> [[[item == x for item in l] for l in lists] 
     for x, lists in zip(main, zip(a, b, c))]
[[[True, False, False], [False, True, False], [False, False, False]], 
 [[False, False, False], [True, False, False], [False, False, False]]]
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195