[Tutor] List comprehension possible with condition statements?

Steven D'Aprano steve at pearwood.info
Wed Mar 3 10:43:43 CET 2010

On Wed, 3 Mar 2010 07:46:39 pm Alan Gauld wrote:

> mylist = [irtem for item in aList where item != None]

Comparisons with None almost always should be one of:

item is None
item is not None

The reason is that "item is None" is ONLY ever true if the item actually 
is the singleton object None (accept no substitutes!).

On the other hand, "item == None" might be true for some customer items. 
So if you actually *do* want to accept substitutes, you can use ==, but 
that would be an unusual thing to do, and worthy of a comment 
explaining that you did mean == and it isn't a mistake.

Likewise for "item != None" versus "item is not None".

Steven D'Aprano

More information about the Tutor mailing list