Skip to content

Instantly share code, notes, and snippets.

@jalcoding8
Last active November 23, 2022 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jalcoding8/9da8afa96f3e3f42e094c5015279fadd to your computer and use it in GitHub Desktop.
Save jalcoding8/9da8afa96f3e3f42e094c5015279fadd to your computer and use it in GitHub Desktop.
Coding-Challenge-Python/Flatten-Array
def flatten_array(arr):
# Write your code here
flat_list = []
# Iterate through the outer list
for element in arr:
#if type(element) is list: #using type() to check for nested array OR
if isinstance(element, list): #using isinstance() to check for nested (multi-dimensional array
# If the element is of type list, iterate through the sublist or inner list
for item in element:
flat_list.append(item)
else:
flat_list.append(element)
return flat_list
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))
print(flatten_array([[2], [8, 9, 10,], 5, 4, []] ))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 8, 9, 10, 5, 4]
All tests passed!
Submit your solution when you're ready. Once you submit, all code will be saved as final.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment