Skip to content

Instantly share code, notes, and snippets.

@alexmacniven
Created June 28, 2021 13:02
Show Gist options
  • Save alexmacniven/6b376d19bdff85bfa6c4712b3fd7903c to your computer and use it in GitHub Desktop.
Save alexmacniven/6b376d19bdff85bfa6c4712b3fd7903c to your computer and use it in GitHub Desktop.
Flatten a List with List Comprehension
"""flatlist
Flatten a list of lists using list comprehension.
"""
from typing import List
nest_list: List = [[1, 2, 3], [4, 5, 6]]
flat_list: List = [item for sublist in nest_list for item in sublist]
assert flat_list == [1, 2, 3, 4, 5, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment