[Coding-Challenge-Python/Flatten-Array · GitHub]
Here is my solution for the Python3 Coding Challenge- Flatten possible multi-dimensional array
I included two possible ways to check is and element in an array is an array:
- type()
- isinstance()
[Coding-Challenge-Python/Flatten-Array · GitHub]
Here is my solution for the Python3 Coding Challenge- Flatten possible multi-dimensional array
I included two possible ways to check is and element in an array is an array:
Here’s a one-liner using the built-in collections
and itertools
modules.
import collections.abc
from itertools import chain
def flatten_array(arr):
return list(chain.from_iterable(n if isinstance(n, collections.abc.Iterable) and
not isinstance(n, str) else [n] for n in arr))
if __name__ == '__main__':
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9])) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(flatten_array([[2], [8, 9, 10, ], 5, 4, []])) # [2, 8, 9, 10, 5, 4]
Hello,
I got the correct output every time I passed an argument to my function, but I kept receiving the following error:
“‘<’ not supported between instances of ‘list’ and ‘int’”. Check your code and try again.
Anyone have an idea of what I am missing?
fyi - As I continued completing the Python-Coding-Challenges I encountered notes from codecademy stating that the challenges should be completed without the use of imports, and itertools was specifically identified.
Consider the slightly different argument,
print(flatten_array([[-1, -2, -3, 100], [1, 2], 2, [11, 0, 3], 6, [7, 8], 9]))
For the above, your code gives the error:
TypeError: ‘<’ not supported between instances of ‘int’ and ‘list’
For the above test case, not all the lists are being flattened. When you call sorted(arr)
, the sorted
function can’t decide how to sort a mixture of integers and lists.
Try adding a print statement(s) as the first line of your loop for debugging purposes. It will help you see what is going on.
def flatten_array(arr):
for i, element in enumerate(arr):
print("Loop variable i:", i)
print("Element being processed:", element)
print("Array:", arr, "\n")
if isinstance(element, list):
arr.extend(element)
arr.pop(i)
return sorted(arr)
the_list = [[1, 2], 3, 4, [ 5, 10, 20], 8]
enumerate is not necessary.neither is pop(), etc…
the flatten (method) function will return a single array (list) of each element and remove all the nested lists
this is the intent.
flattened_list = [1, 2, 3, 4, 5, 10, 20, 8] (because the_list elements will be appended to falttened_list by function)
enumerate will return a list of tuples like so:
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 10), (6, 20), (7, 8)]
that is not a flattened list
I’m not sure this is the information you want.
Where is the error you receive?
I ran the code on the error you received and got the correct output.
[-3, -2, -1, 0, 1, 2, 3, 6, 7, 8, 9, 11, 100]
With which of the two arguments did you test your code?
flatten_array([[-1, -2, -3, 100], [1, 2], 2, [11, 0, 3], 6, [7, 8], 9])
# OR
flatten_array([[-1, -2, -3, 100], 1, 2, [11, 0, 3], 6, [7, 8], 9])