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 vs ifilter

    By admin on Jan 24, 2016

    Python filter is very handy python built-in function to filter iterables (e.g. lists) in python. But it returns a list which may consume huge memory when data behind iterable is huge. In such case using itertools.ifilter is a better choice. itertools.ifilter takes a lazy approach and calls the filter function only when needed. Here are quick examples for both approaches.
    python-filter-vs-ifilter

    filter example

    Here filter function is called for all values the moment filter is called.

    a = [1,101, 2,3]
    def is_lt_100 (x):
      print "is_lt_100 called"
      return x < 100
    b = filter(is_lt_100, a)
    for x in b:
      print "x=%s" % (x)
    
    is_lt_100 called
    is_lt_100 called
    is_lt_100 called
    is_lt_100 called
    x=1
    x=2
    x=3
    
    Env: Python 2.7.16

    itertools.filter example

    Here ifilter function is called only when we use the iterator returned by ifilter.

    import itertools
    a = [1,2,101,3]
    def is_lt_100 (x):
      print "is_lt_100 called"
      return x < 100
    b = itertools.ifilter(is_lt_100, a)
    for x in b:
      print "x=%s" % (x)
    
    is_lt_100 called
    x=1
    is_lt_100 called
    x=2
    is_lt_100 called
    is_lt_100 called
    x=3
    
    Env: Python 2.7.16

    Suggested posts:

    1. Python itertools ifilter examples
    2. Python filter list/iterable examples
    3. Python itertools imap examples
    4. Python map examples
    5. python print examples
    6. Python for loop examples
    7. WordPress – how to add filter to description meta tag
    8. WordPress – add content filter after shortcode execution
    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 © 2022 InfoHeap.

    Powered by WordPress