4

This question is very much like: if/else in Python's list comprehension? and Simple syntax error in Python if else dict comprehension . But still i dont understand what error I make here:

[i if i!=0 for i in range(2)]
             ^
       syntax error

I only want the entries in the list that are non-zero for sparsity.

Community
  • 1
  • 1
Leo
  • 1,757
  • 3
  • 20
  • 44

3 Answers3

12

Move the if to the end. Refer to The Python Docs entry on List Comprehensions.

>>> [i for i in range(2) if i!=0] # Or [i for i in range(2) if i]
[1]

If you were looking for a conditional expression, you could do something like @Martijn pointed out,

>>> [i if i!=0 else -1 for i in range(2)]
[-1, 1]

If you just want the non zero entities, you could also filter(...) your list.

>>> filter(None, [1, 2, 0, 0, 4, 5, 6])
[1, 2, 4, 5, 6]
Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
  • 1
    Thank you for that link to http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions – Leo Aug 15 '13 at 20:00
  • @Leo : Glad to help. Feel Free to accept the answer when the system allows you to. :) – Sukrit Kalra Aug 15 '13 at 20:00
  • Yes i was planning to, 7 more minutes. I dont understand why comprehensions need the same order as loops though. Just convention? – Leo Aug 15 '13 at 20:03
  • 1
    Comprehensions clauses require the order they do because the syntax was calqued from set comprehensions in math, which use that order, and because they're almost entirely equivalent to loops, so it makes sense to make them look alike. – user2357112 Aug 15 '13 at 20:08
  • 1
    or `[i if i!=0 else -1 for i in range(2)]` perhaps? The `if` could have been an incomplete conditional expression. – Martijn Pieters Aug 15 '13 at 20:08
  • @MartijnPieters : Thanks. Added that to the answer. :) – Sukrit Kalra Aug 15 '13 at 20:11
  • @SukritKalra: it is called a [conditional expression](http://docs.python.org/2/reference/expressions.html#conditional-expressions); you can link to the documentation perhaps. – Martijn Pieters Aug 15 '13 at 20:14
  • I don't understand last `filter(None, [1, 2, 0, 0, 4, 5, 6])` why `None` remove all `0`s **?** What I understand first argument to `filter()` is condition and `bool(None)` == False so it should return empty list. – Grijesh Chauhan Aug 24 '13 at 10:01
  • @GrijeshChauhan : Refer to [The Python Docs](http://docs.python.org/2/library/functions.html#filter), Quoted, `Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None.` – Sukrit Kalra Aug 24 '13 at 10:04
  • @SukritKalra Oh I also read: `filter(function, iterable)` : *If function is None, the identity function is assumed, that is, all elements of iterable that are* *`false`* *are removed.* But What is `identity function` ? – Grijesh Chauhan Aug 24 '13 at 10:10
  • The identity function checks if the item is truth-y or not. – Sukrit Kalra Aug 24 '13 at 10:26
2

The if predicate comes after the specification of the for i in range(2) in a list comprehension. You can also have arbitrary number of ifs.

1

Switch the if i!=0 and for i in range(2) parts:

>>> [i for i in range(2) if i!=0]
[1]
>>>