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?

2
  • 1
    It's not really clear how [False, True, False] relates to [2, 1, 3] Commented Jan 24, 2016 at 20:52
  • Your example lists are missing some commas. And True and False should be upper case. Commented Jan 24, 2016 at 22:31

1 Answer 1

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]]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.