Complementary function filterfalse, ifilterfalse

suggest change

There is a complementary function for filter in the itertools-module:

# not recommended in real use but keeps the example valid for python 2.x and python 3.x
from itertools import ifilterfalse as filterfalse
from itertools import filterfalse

which works exactly like the generator filter but keeps only the elements that are False:

# Usage without function (None):
list(filterfalse(None, [1, 0, 2, [], '', 'a']))  # discards 1, 2, 'a' 
# Out: [0, [], '']

Usage with function

names = [Fred, Wilma, Barney]

def long_name(name):
	return len(name) > 5

list(filterfalse(long_name, names)) # Out: [‘Fred’, ‘Wilma’]

Short-circuit usage with next:

car_shop = [('Toyota', 1000), ('rectangular tire', 80), ('Porsche', 5000)]
def find_something_smaller_than(name_value_tuple):
    print('Check {0}, {1}$'.format(*name_value_tuple)
    return name_value_tuple[1] < 100
next(filterfalse(find_something_smaller_than, car_shop))
# Print: Check Toyota, 1000$
# Out: ('Toyota', 1000)

Using an equivalent generator:

car_shop = [('Toyota', 1000), ('rectangular tire', 80), ('Porsche', 5000)] 
generator = (car for car in car_shop if not car[1] < 100)
next(generator)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Filter:
* Filter
* Complementary function filterfalse, ifilterfalse

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve