I'm trying to flatten some mixed arrays in Python using LC. I'm having some trouble figuring out how to structure it.
Here's the array's i'm trying to flatten
arr_1 = [1, [2, 3], 4, 5]
arr_2 = [1,[2,3],[[4,5]]]
I tried this methods for arr_1 but get "TypeError: 'int' object is not iterable"
print([item if type(items) is list else items for items in arr_1 for item in items])
So I decided to break it into parts to see where it's failing by using this
def check(item):
return item;
print([check(item) if type(items) is list else check(items) for items in [1, [2, 3], 4, 5] for items in arr_2])
Through the debugger I found that it's failing at the 2d array in
for items in [1, [2, 3], 4, 5]
I don't need the LC to be in one line but I just wanted to know how to do it in a single nested LC if its even possible.