The reason that you get the error is that loops in a double comprehension like that are written in the same order as a for loop. In particular,
a = []
for y in z:
for x in y:
a.append(x)
could be rewritten as
a = [x for y in z for x in y]
NOT as
a = [x for x in y for y in z]
The syntax you are probably thinking of is for a nested comprehension:
a = [[x for x in y] for y in z]
But that creates a 2D list, which is definitely not what you want here.
Now to answer your actual question, let's start with items[1] for _ in range(len(items))
. You are literally checking items[1]
twice for each element in your list. There may have been an attempt to ensure that the sub-list has two elements, but you killed it when you explicitly indexed element 1
.
Now you'd think that you can truncate your comprehension to
allscores = [items[1] for items in students if items[1] not in allscores]
But you can't. Remember that until the comprehension completes, allscores
is undefined. Pre-initializing it to an empty list won't help either because it will stay empty until the comprehension completes.
The solution is to use a set
. It's much more efficient than the linear lookup you are trying to do every time with the in
operator in if items[1] not in allscores
. It can also be used with a generator expression instead of a list comprehension to avoid storing the unnecessary elements:
allscores = set(items[1] for items in students)
Notice the lack of surrounding square brackets. If you absolutely need a list, do
allscores = list(set(items[1] for items in students))