2

Can someone please explain the meaning the syntax behind the following line of code:

temp3 = [x for x in temp1 if x not in s]

I understand it's for finding the differences between 2 lists, but what does the 'x' represent here? Each individual element in the list that is being compared? I understand that temp1 and s are lists. Also, does x for x have to have the same variable or could it be x for y?

pHorseSpec
  • 1,246
  • 5
  • 19
  • 48

4 Answers4

5
[x for x in temp1 if x not in s]

It may help to re-order it slightly, so you can read the whole thing left to right. Let's move the first x to the end.

[for x in temp1 if x not in s yield x]

I've added a fake yield keyword so it reads naturally as English. If we then add some colons it becomes even clearer.

[for x in temp1: if x not in s: yield x]

Really, this is the order that things get evaluated in. The x variable comes from the for loop, that's why you can refer to it in the if and yield clauses. But the way list comprehensions are written is to put the value being yielding at the front. So you end up using a variable name that's not yet defined.

In fact, this final rewrite is exactly how you'd write an explicit generator function.

def func(temp1, s):
    for x in temp1:
        if x not in s:
            yield x

If you call func(temp1, s) you get a generator equivalent to the list. You could turn it into that list with list(func(temp1, s)).

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
3

It iterates through each element in temp1 and checks to see if it is not in s before including it in temp3.

It is a shorter and more pythonic way of writing

temp3 = []
for item in temp1:
    if item not in s:
        temp3.append(item)

Where temp1 and s are the two lists you are comparing.

As for your second question, x for y will work, but probably not in the way you intend to, and certainly not in a very useful way. It will assign each item in temp1 to the variable name y, and then search for x in the scope outside of the list comprehension. Assuming x is defined previously (otherwise you will get NameError or something similar), the condition if x not in s will evaluate to the same thing for every item in temp1, which is why it’s not terribly useful. And if that condition is true, your resulting temp3 will be populated with xs; the y values are unused.

Do not take this as saying that using different variables in a list comprehension is never useful. In fact list comprehensions like [a if condition(x) else b for x in original_sequence] are often very useful. A list comprehension like [a for x in original_sequence if condition(x)] can also be useful for constructing a list containing exactly as many instances of a as the number of items in original_sequence that satisfy condition().

taylor swift
  • 2,039
  • 1
  • 15
  • 29
0

Try yourself:

arr = [1,2,3]
[x+5 for x in arr]

This should give you [6, 7, 8] that are the values on the [1,2,3] list plus 5. This syntax is know as list comprehension (or mapping). It applies the same instructions to all elements on a list. Would be the same as doing this:

for x in arr:
    arr += 5
user6549300
  • 184
  • 5
-2

X is same variable and it is not y. It works same as below code

newList = []
for x in temp1:
   if x not in s:
   newList.append(x)

So x for x, here first is x which is inside append in code and x after for is same as for x in temp1.

bhansa
  • 7,282
  • 3
  • 30
  • 55
Rakesh Kumar
  • 4,319
  • 2
  • 17
  • 30