itertools.ifilter Vs. filter Vs. list comprehensions

后端 未结 4 2009
孤城傲影
孤城傲影 2020-12-30 22:25

I am trying to become more familiar with the itertools module and have found a function called ifilter.

From what I understand, it filters

4条回答
  •  星月不相逢
    2020-12-30 22:50

    Your understanding is corret: the only difference is that ifilter returns an iterator, while using filter is like calling:

    list(ifilter(...))
    

    You may also be interested in what PEP 289 says about filter and ifilter:

    List comprehensions greatly reduced the need for filter() and map(). Likewise, generator expressions are expected to minimize the need for itertools.ifilter() and itertools.imap(). [...]

    Also note that ifilter became filter in Python-3 (hence removed from itertools).

提交回复
热议问题