Skip to content

Instantly share code, notes, and snippets.

@raeq
Created August 9, 2020 21:46
Show Gist options
  • Save raeq/71072426db3c453a6a07327cf8311c03 to your computer and use it in GitHub Desktop.
Save raeq/71072426db3c453a6a07327cf8311c03 to your computer and use it in GitHub Desktop.
Flatten a list of lists to a list.
from itertools import chain
def flatten_nested_lists(*input_list: list) -> list:
"""flatten_nested_lists.
Args:
input_list:
Returns:
list:
>>> l1: list = []
>>> l1.append([1, "2", 3])
>>> l1.append([4, 5, 6, 7])
>>> l1.append(["Eight", {"this one": 9}])
>>> l1.append([10, 11, 12])
>>> print(list(flatten_nested_lists(*l1)))
[1, '2', 3, 4, 5, 6, 7, 'Eight', {'this one': 9}, 10, 11, 12]
"""
for i in chain.from_iterable(input_list):
yield i
l2: list = []
l2.append([1, 2, 3])
l2.append([4, 5, 6])
l2.append([10, 11, 12])
for list_item in flatten_nested_lists(*l2):
print(list_item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment