Skip to content

Latest commit

 

History

History
21 lines (14 loc) · 853 Bytes

filter.md

File metadata and controls

21 lines (14 loc) · 853 Bytes

Filter

Before reading this article, please make sure you understand how the lambda function works.

Like zip() and map(), filter can create and return a generator. generator is an implementation of iterator with lazy evaluation features, which can reduce memory usage very effectively.

Table of Contents

Basic

The filter() function retrieves the desired value from the list and removes the unnecessary value. For example, in the following example we want to find the names of people whose names start with “O” in the list.

names = ['Liam', 'Olivia', 'Noah', 'Emma', 'Oliver', 'Ava']
choice = filter(lambda x: x.startswith('O'), names)

print(*choice, sep=', ')  # Olivia, Oliver