Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The functionality of <code>map</code> and <code>filter</code> was intentionally changed to return iterators, and reduce was removed from being a built-in and placed in <code>functools.reduce</code>.</p> <p>So, for <code>filter</code> and <code>map</code>, you can wrap them with <code>list()</code> to see the results like you did before.</p> <pre><code>&gt;&gt;&gt; def f(x): return x % 2 != 0 and x % 3 != 0 ... &gt;&gt;&gt; list(filter(f, range(2, 25))) [5, 7, 11, 13, 17, 19, 23] &gt;&gt;&gt; def cube(x): return x*x*x ... &gt;&gt;&gt; list(map(cube, range(1, 11))) [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] &gt;&gt;&gt; import functools &gt;&gt;&gt; def add(x,y): return x+y ... &gt;&gt;&gt; functools.reduce(add, range(1, 11)) 55 &gt;&gt;&gt; </code></pre> <p>The recommendation now is that you replace your usage of map and filter with generators expressions or list comprehensions. Example:</p> <pre><code>&gt;&gt;&gt; def f(x): return x % 2 != 0 and x % 3 != 0 ... &gt;&gt;&gt; [i for i in range(2, 25) if f(i)] [5, 7, 11, 13, 17, 19, 23] &gt;&gt;&gt; def cube(x): return x*x*x ... &gt;&gt;&gt; [cube(i) for i in range(1, 11)] [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] &gt;&gt;&gt; </code></pre> <p>They say that for loops are 99 percent of the time easier to read than reduce, but I'd just stick with <code>functools.reduce</code>.</p> <p><strong>Edit</strong>: The 99 percent figure is pulled directly from the <a href="https://docs.python.org/3.0/whatsnew/3.0.html" rel="noreferrer" title="What’s New In Python 3.0">What’s New In Python 3.0</a> page authored by Guido van Rossum.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload