InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Python

    Python strings

    • Python string quick start tutorial
    • Python string split examples
    • Python string lowercase and uppercase
    • Python string contains check
    • int to string
    • string to int

    Python List (Array)

    • list basics
    • list append, prepend
    • list remove an item
    • merge lists
    • slice of list
    • array clone
    • initialize list with same value
    • value in list check

    Python Dictionary

    • dictionary basics
    • Python iterate dictionary
    • dictionary get key with default
    • sort dictionary by values

    Python control flow

    • if-elif-else
    • for loop

    Python Regex

    • re search() vs match()
    • re.match()
    • re.search()
    • re.sub()

    Python Cookbook

    • Command line - run python webserver
    • How to find python package's file location
    • Python etl petl - read table from csv file
    • Python file read write examples

    Python built-in functions

    • filter
    • filter vs ifilter
    • itertools ifilter
    • itertools imap
    • len
    • map
    • print
    • range
    • rstrip
    • type
    • xrange
     
    • Home
    • > Tutorials
    • > Python

    Python filter list/iterable examples

    By admin on Jan 23, 2016

    Python filter is very useful built-in python function to achieve complex stuff over a list in very short and elegant way. Note that filter applies to any iterable (e.g. list). We’ll use list for the purpose of this tutorial.
    python-filter

    Usage: filter(function, iterable)
    The function should take one argument and should return boolean value. If true is returned for a value, that value is taken.

    Here are some examples.

    filter a list by applying a function

    Usage: filter(function, iterable)
    Given a list of numbers get a new list with items less than 100.

    def is_lt_100(x):
      return x < 100
    
    a = [1,2,3,4,1000]
    b = filter(is_lt_100, a)
    print b
    [1, 2, 3, 4]
    
    Env: Python 2.7.16

    filter a list by applying a lambda function

    Given a list of numbers get a new list with items less than 100 using lambda function.

    a = [1,2,3,4,1000]
    b = filter(lambda x: x < 100, a)
    print b
    [1, 2, 3, 4]
    
    Env: Python 2.7.16

    filter a list using [x for x in list if ()]

    a = [1,2,3,4,1000]
    b = [x for x in a if (x < 100)]
    print b
    [1, 2, 3, 4]
    
    Env: Python 2.7.16

    filter a list to get non empty value

    Filter a list to get non empty values. We can use either None value as function (it will use identity function which will return false for empty value) or bool function.

    a = [0,1,0,2]
    b = filter(None, a)
    print b
    c = filter(bool, a)
    print c
    [1, 2]
    [1, 2]
    
    Env: Python 2.7.16

    filter a list and get iterable

    This approach will be more memory efficient for large iterable. For big data and ETL operations, we should use this approach.

    import itertools
    a = [1,2,3,4,1000]
    b = itertools.ifilter(lambda x: x < 100, a)
    print b
    print [x for x in b]
    <itertools.ifilter object at 0x7fac0ab80810>
    [1, 2, 3, 4]
    
    Env: Python 2.7.16

    Suggested posts:

    1. Python – How to sort dictionary by values
    2. python print examples
    3. Python for loop examples
    4. Python filter vs ifilter
    5. Python map examples
    6. Python itertools ifilter examples
    7. Python itertools imap examples
    8. Python range examples
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Python, Python array, Python built-in functions, Tutorials

    Follow InfoHeap

    facebook
    twitter
    googleplus
    • Browse site
    • Article Topics
    • Article archives
    • Recent Articles
    • Contact Us
    • Omoney
    Popular Topics: AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

    Copyright © 2021 InfoHeap.

    Powered by WordPress