TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python filter (Lambda Removes From List or Range)

Use the filter built-in method to modify lists of numbers. Filter a range in this way.
Filter. This removes non-matching elements. Internally it loops over the list (or other iterable). When the lambda predicate returns False, the element is removed.
With filter, we use functional programming—we focus on function calls with lambda arguments, not for-loops with if-statements. This is powerful, but can be confusing too.Built-ins
List example. Here we use filter with the list type. Almost all Python programs use lists, so this is a good place to start. We use the lambda keyword.Lambda

Result: The filter() built-in does not return another list. It returns an iterator. We can use the list() function to create a new list.

List
Python program that uses filter numbers = [10, 20, 0, 0, 30, 40, -10] # Filter out numbers equal to or less than zero. result = list(filter(lambda n: n > 0, numbers)) print(result) Output [10, 20, 30, 40]
Range example. The filter() method can be used with a range as the second argument. Here we have a range from 0 through 9 inclusive. We then call filter on this range.

Lambda: The lambda receives each value in the range and names it "x." If it is evenly divisible by 2, it returns true.

Range

So: The lambda filters out all numbers that are not evenly divisible by 2 (all odd numbers). We are left with even numbers.

Python program that uses filter with range # Use a range with filter. # ... Return true in lambda if number is even. # Even numbers are all evenly divisible by 2. # We then print all even numbers in the range with filtering. for value in filter(lambda x: x % 2 == 0, range(0, 10)): print("EVEN NUMBER IN RANGE:", value); Output EVEN NUMBER IN RANGE: 0 EVEN NUMBER IN RANGE: 2 EVEN NUMBER IN RANGE: 4 EVEN NUMBER IN RANGE: 6 EVEN NUMBER IN RANGE: 8
A note. When using filter, we must specify what elements we want to keep—all others are removed. So when the test succeeds in the lambda (and it returns true) we keep the element.
A review. Filter() is a useful built-in. But as with other functional constructs, it is best to use it only when it makes code clearer. A for-loop may be preferred for simplicity.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf