-3

I am trying to get a grasp of list comprehension and ran into a problem that throws a syntax error.

Here I'm trying to get a list of odd number:

ll = []
for each in l:
    if each%2 == 1:
        ll.append(each)
    else:
        pass
ll
>>> [1, 3, 5]

Using list comprehension, however, this throws a syntax error at pass:

l = [1,2,3,4,5]
[each if each%2==1 else pass for each in l]
>>> [each if each%2==1 else pass for each in l]
                            ^
>>> SyntaxError: invalid syntax

If I were to replace pass with something like 0, it would work and return [1,0,3,0,5] without throwing an error. Could someone explain why I can't use pass here?

user9532692
  • 584
  • 7
  • 28
  • 2
    What tutorial about list comprehensions doesn't cover if-clauses? – Kelly Bundy Jan 22 '23 at 05:29
  • I'm reading through tutorials about list comprehensions and the example in the tutorial has `0` instead of `pass` in there. So I wondered if I could just use `pass` in the else clause to return [1,3,5] instead of [1,0,3,0,5]. But it didn't return the result I expected - which is why I posted this question. – user9532692 Jan 22 '23 at 05:32
  • If you're aware of any tutorial that answer the question I have here, I'd greatly appreciate your reference. – user9532692 Jan 22 '23 at 05:33
  • @PranavHosangadi oops, that isn't the right duplicate for this. I don't know that I have an appropriate one off hand, but I am quite confident this is well covered. - Okay, I found one that is **accurate**, if not necessarily the best canonical. – Karl Knechtel Jan 22 '23 at 05:39
  • I meant why are you even trying to do this instead of the correct way. – Kelly Bundy Jan 22 '23 at 05:47
  • @KarlKnechtel https://stackoverflow.com/q/59115295/12671057 – Kelly Bundy Jan 22 '23 at 05:51
  • @KellyBundy At the time I was posting this question, I knew I could do `[each for each in l if each%2 ==1]` to return the result I want. What I was confused was using `if else` clause before the for-loop statement in the list comprehension - which @Equinox kindly explained the difference in the comments below. – user9532692 Jan 22 '23 at 05:52
  • @user9532692 I don't think Equinox really explained it. The answer in the linked question does (I found it by googling with "pass is a statement"). – Kelly Bundy Jan 22 '23 at 05:55
  • @KarlKnechtel This question is "why I can't use pass here?". Your duplicate isn't even talking about `pass`. – Kelly Bundy Jan 22 '23 at 06:02
  • @KellyBundy it's a simple task and I linked the how-to for that task. Explanations of why specific wrong attempts don't work, belong as additional content in new answers there. – Karl Knechtel Jan 22 '23 at 06:09

1 Answers1

0

you don't need even need to write else pass. It basically does nothing.

So simply just think of it as using an if statement

>>> l = [1,2,3,4,5]
>>> [each for each in l if each%2 ==1]
[1, 3, 5]
Equinox
  • 6,483
  • 3
  • 23
  • 32
  • Yeah this is something that confuses me as well. Sometimes, I see that the if clause being used after the for-loop statement, whereas in some cases, if-else clause is used between the result being returned (`each` in this case) and the for-loop statement. – user9532692 Jan 22 '23 at 05:38
  • well there are 2 syntaxes when you have just `if` `[ expression for item in list if conditional ]` and when you have `if/else` `[ expression if conditional else value for item in list ]` ` – Equinox Jan 22 '23 at 05:41
  • The reason that I posted the question was to understand why `else pass` in the list comprehension would throw a syntax error, whereas `else \n pass` works in the for-loop above. – user9532692 Jan 22 '23 at 05:41
  • because `pass` does nothing. It isn't a variable/value that the list comprehension expects. and what pass does is implicitly implemented in the list comprehension syntax, that is to skip the element and do nothing. – Equinox Jan 22 '23 at 05:43