Reputation: 1253
I have a list of lists like this,
big_list = [[1,3,5], [1,2,5], [9,3,5]]
sec_list = [1,3,5]
I want to iterate through the big_list
and check each list values against the sec_list
. While I check, I want to store the values that are not matching into a another list of lists. So, I did this:
sma_list = []
for each in big_list:
for i,j in zip(each, sec_list):
if i!=j:
sma_list.append(i)
I get the result like this:
[2, 9]
However, I need a list of lists like this,
[[2], [9]]
How can I achieve this?
Upvotes: 5
Views: 131
Reputation: 114310
Short answer,
sma_list.append([i])
Enclosing a value in square brackets makes it the first element of a one element list.
This will only work correctly if you have one missing element per list. You're much better off using a comprehension for the whole thing:
sma_list = [[i for i, j in zip(each, sec_list) if i != j] for each in big_list]
Upvotes: 4