1

As a part of learning python map/reduce/filter methods I've got an exercise- to convert function to one line code using map/reduce/filter only. This is the function that need to be converted:

def func5(n):
    l = []
    for i in range(n):
        j = 0
        while j < i:
            if j % 2 == 0:
                l.append(j + 5)
            elif j % 3 == 0:
                l.append(j // 2)
            elif j % 5 == 2:
                l.append(j)
            j += 1
    return l

So I think that I've done the if-else correct, but I don't know what is the syntax to create nested loop in lambda commands (we should not use for loops). My code so far:

l = list(map(lambda x: x+5 if x % 2 ==0 else x//2 if x % 3 ==0 else x if x % 5 ==2 ,_______))
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Saar Gamzo
  • 57
  • 4

2 Answers2

1

All-in in a single line as required... reduce to concatenate lists (by default available only in the functools module, not anymore as build-in), filter to remove the side effects of the inline conditions (see my comment).

import functools as fc
n = 10
a = fc.reduce(lambda i, j: i+j, map(lambda i: list(filter(None, map(lambda j: j+5 if j%2==0 else (j//2 if j%3==0 else (j if j%5==2 else None)), range(i)))), range(n)))

a = list(a)
print(a)
print(a == func5(n))

Output

[5, 5, 5, 7, 5, 7, 1, 5, 7, 1, 9, 5, 7, 1, 9, 5, 7, 1, 9, 11, 5, 7, 1, 9, 11, 7, 5, 7, 1, 9, 11, 7, 13]
True
cards
  • 3,936
  • 1
  • 7
  • 25
  • Thank you! Do you think there is an option to "replace" the "if-else" commands with any other command? – Saar Gamzo Apr 20 '22 at 15:13
  • @Saar Gamzo you could try to play around with `conditions = lambda j: {True: None, j % 2 == 0: j+5, j % 3 == 0: j // 2, j % 5 == 2: j} ` – cards Apr 20 '22 at 19:55
1

In list comprehension form:

[
    x
    for i in range(n)
    for j in range(i)
    for x in (
        [j + 5]
        if j % 2 == 0
        else [j // 2]
        if j % 3 == 0
        else [j]
        if j % 5 == 2
        else []
    )
]

...which can be converted to two reduces:

reduce(
    lambda xs, i: xs
    + reduce(
        lambda ys, j: ys
        + (
            [j + 5]
            if j % 2 == 0
            else [j // 2]
            if j % 3 == 0
            else [j]
            if j % 5 == 2
            else []
        ),
        range(i),
        [],
    ),
    range(n),
    [],
)
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • Thank you! Do you think there is an option to "replace" the "if-else" commands with any other command? – Saar Gamzo Apr 20 '22 at 15:13
  • There's a couple of alternatives listed here: [https://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator](https://stackoverflow.com/q/394809/365102). – Mateen Ulhaq Apr 20 '22 at 20:08