0

I have the following problem, transform the structure below

[[1], [2,3,4,5,6], [7,8,9]]

by getting items from nested lists, as a result

[1,2,3,4,5,6,7,8,9]

The simplest implementation can be written in the following iterative way:

tab = [[1], [2,3,4,5,6], [7,8,9]]
result = []

for x in tab:
    for y in x:
        result.append(y)

I wondering, how to implement following functionality in paradigms of functional programming (for example using map). Any ideas? Thank you in advance for your help

petezurich
  • 9,280
  • 9
  • 43
  • 57
Costy
  • 165
  • 2
  • 14
  • how about: `list(itertools.chain.from_iterable(lst))` –  Apr 13 '22 at 19:24
  • You can't use `map()` for this. It always produces the same number of outputs as inputs. You could use `reduce()`. The callback function can concatenate lists with `accumulator + current` – Barmar Apr 13 '22 at 19:29
  • 3
    `[val for row in tab for val in row]` – trincot Apr 13 '22 at 19:30
  • from functools import reduce; print(list(reduce(list.__add__, [[1], [2,3,4,5,6], [7,8,9]]))) – DarkKnight Apr 13 '22 at 19:43

0 Answers0