Editor

Flatten One Level

#26 · Lists · Beginner · 25 XP · about 4 min

You have a list of lists. Write flatten(groups) that returns one list holding every item from every inner list, in order: all of the first inner list, then all of the second, and so on.

Flatten only one level. If an inner list contains another list, that list stays as a single item.

Examples

flatten([[1, 2], [3], [4, 5, 6]])  # returns [1, 2, 3, 4, 5, 6]
flatten([[], ['a'], []])           # returns ['a']
flatten([[1, [2, 3]], [4]])        # returns [1, [2, 3], 4]

In the last example [2, 3] is an item of the first inner list, so it stays a list.

Ready?

Accept the challenge to start writing. A small timer counts the time this page is in view.

Output appears here.