Use map and filter instead of a for loop?
<p>How do I write an equivalent of the code below using map and filter?</p>

<pre><code>res = []
for x in range(5):
    if x % 2 == 0:
        for y in range(5):
            if y % 2 == 1:  
                res.append((x, y))
</code></pre>

<p>This is the expected result:</p>

<pre><code>[(0, 1), (0, 3), (2, 1), (2, 3), (4, 1), (4, 3)]
</code></pre>

<p>Here's the code I wrote, but it doesn't seem to work:</p>

<pre><code>list( map(( lambda x,y: (x,y)), filter((lambda x: x%2 == 0), range(5)), filter((lambda y: y%2 != 0), range(5))))
</code></pre>

<p>You need to use <a href="https://docs.python.org/3/library/itertools.html#itertools.product" rel="nofollow"><code>product</code></a> method from <a href="https://docs.python.org/3/library/itertools.html" rel="nofollow"><code>itertools</code></a>, which produces cartesian product of input iterables.</p>

<pre><code>x_arr = [x for x in range(5) if x % 2 == 0]
y_arr = [y for y in range(5) if x % 2 == 1]
from itertools import product
final_arr = product(x_arr, y_arr)
</code></pre>

<p>Output would be <code>[(0, 1), (0, 3), (2, 1), (2, 3), (4, 1), (4, 3)]</code></p>

python dictionary filter functional-programming