Created
June 28, 2021 13:02
-
-
Save alexmacniven/6b376d19bdff85bfa6c4712b3fd7903c to your computer and use it in GitHub Desktop.
Flatten a List with List Comprehension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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