I\'ve seen the following code many times, and I know it\'s the solution to my problems, but I\'m really struggling to understand HOW it works. The code in particular is:
It might be clearer if you split it into three parts:
item
;for item in list
;item not in list_b
.The reason the list comprehension syntax is like this is firstly because that reflects the expanded version:
for item in list: # 2.
if item not in list_b: # 3.
new_list.append(item) # 1.
and also because you don't necessarily want just item
, for example:
new = [x ** 2 for x in old if not x % 2]
will create a new
list with the squares of all even numbers in old
.
The list comprehension [item for item in list_a if item not in list_b]
is equivalent to using the filter
function:
filter(list_a, lambda item: item not in list_b)
You are building a list by looping through(and filtering) another list
The first item is the thing you are putting into your result list
You can also do stuff to the element such as
[i*2 for i in range(6)]
The output will be:
[0, 2, 4, 6, 8, 10]
Essentially the first "item" lets you control what you put into your result list
First off, the line of code you're referring to is called a list comprehension. Basically, as you know, it is a way to create a list of items "on the go", with the ability for conditionals as well.
When you create a list comprehension, you are building a list, and you need to to tell Python what you are putting into that list. If a list comprehension was simply:
new = [for item in list_a if item not in list_b]
It would not be a list comprehension at all, because you are simply iterating and not storing any thing in the new list. So, in order to actually save items into the new list, you need:
new = [item for item in list_a if item not in list_b]
# for each item in 'list_a', if 'item' is not in
# 'list_b', add item to 'new'
Which is the same as:
i = 0
for item in list_a:
if item not in list_b:
new[i++] = item
Think of "that first item
" as the value you are putting into the list. Essentially a list comprehension enumerates, keeping track of the index and does something like this for each iteration:
new[index] = item