Files
30-seconds-of-code/snippets/flatten.md
2022-12-04 22:24:48 +02:00

401 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Flatten list list blog_images/jars-on-shelf.jpg 2019-09-19T15:46:10+03:00 2020-11-02T19:27:53+02:00

Flattens a list of lists once.

  • Use a list comprehension to extract each value from sub-lists in order.
def flatten(lst):
  return [x for y in lst for x in y]
flatten([[1, 2, 3, 4], [5, 6, 7, 8]]) # [1, 2, 3, 4, 5, 6, 7, 8]