python-cheatsheet
python-cheatsheet copied to clipboard
List flattening does not work properly
import itertools
nested_list1 = ["s", "r", [32, [32], 3]]
flattened_list1 = list(itertools.chain.from_iterable(nested_list1))
print(flattened_list1)
nested_list2 = [1, [1, 1, 3, [224, 4], []], [], [2, [23]]]
flattened_list2 = list(itertools.chain.from_iterable(nested_list2))
print(flattened_list2)
# RESULT
# ['s', 'r', 32, [32], 3] (flattened_list) <-- not completely flat
# TypeError: 'int' object is not iterable (flattened_list2) <-- doesnt work at all
I suggest:
from copy import deepcopy
def flatten(nested):
"""Flatten an arbitrarily nested list."""
nested = deepcopy(nested)
flat = []
while nested:
sublist = nested.pop(0)
if isinstance(sublist, list):
nested = sublist + nested
else:
flat.append(sublist)
return flat
list1 = flatten(nested_list1)
list2 = flatten(nested_list2)
print(list1)
print(list2)
# RESULT
# ['s', 'r', 32, 32, 3] (list1)
# [1, 1, 1, 3, 224, 4, 2, 23] (list2)
Thanks. Yes, it's not perfect but it fits on one line, so all I could really do, is remove the example. I mean your function is pretty neat, but it would clutter the List section.
Well you could also put the function somewhere else, not where the original was.
Just saying, but nested = nested[:] should do the same as nested = deepcopy(nested) but without the import. Basically this says the new nested should equal the values of the old nested (copy by value), in contrast to nested = nested which is copy by reference.
Thanks :)
import itertools
nested_list1 = ["s", "r", [32, [32], 3]]
flattened_list1 = list(itertools.chain.from_iterable(nested_list1))
print(flattened_list1)
nested_list2 = [1, [1, 1, 3, [224, 4], []], [], [2, [23]]]
flattened_list2 = list(itertools.chain.from_iterable(nested_list2))
print(flattened_list2)
# RESULT
# ['s', 'r', 32, [32], 3] (flattened_list) <-- not completely flat
# TypeError: 'int' object is not iterable (flattened_list2) <-- doesnt work at all
I suggest:
import itertools
def flatten_list(nested_list):
flattened_list = []
for item in nested_list:
if isinstance(item, list):
flattened_list.extend(flatten_list(item))
else:
flattened_list.append(item)
return flattened_list
I changed 'flattened_list' to 'flatter_list'. This issue should have been closed long ago. Thanks for the input but there's no space left (except for a few additional lines in Coroutines and Plotly).