This community-built FAQ covers the “Flatten an Array” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the Python challenge Flatten an Array

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like () to up-vote the contribution!

def flatten_array(arr):
flattened =
for i in arr:
if isinstance(i, list) == True:
for a in i:
flattened.append(a)
else:
flattened.append(i)
return flattened

def flatten_array(arr):
# Write your code here
new_arr = []
for i in arr:
if isinstance(i, list):
new_arr += [x for x in i]
else:
new_arr.append(i)
return new_arr
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))

def flatten_array(arr):
lst = []
for a in arr:
if type(a) == int:
lst.append(a)
elif type(a) == list:
for i in a:
lst.append(i)
return lst
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))

def flatten_array(arr):
# Write your code here
lists = []
for i in range(len(arr)):
if type(arr[i]) != list:
lists.append(arr[i])
else:
for i2 in range(len(arr[i])):
lists.append(arr[i][i2])
return lists
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))

def flatten_array(arr):
ret = []
for i in arr:
try:
for j in i:
ret.append(j)
except:
ret.append(i)
return ret
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))

def flatten_array(arr):
# Write your code here
z=[]
for i in arr:
if type(i)==int:
z.append(i)
else:
for b in i:
z.append(b)
return z
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))

def flatten_array(arr):
# Write your code here
result = []
for i in arr:
if type(i) != int:
for j in i:
result.append(j)
else:
result.append(i)
return result
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))

Same comment as a couple of posts above… What if the array contains strings or some other numeric value like a float? The type of content is not important, only whether it is a list or not.

def flatten_array(arr):
# Write your code here
flattened=[]
for x in arr:
try:
for y in range(len(x)):
flattened.append(x[y])
except:
flattened.append(x)
return(flattened)
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))

Hi, I got this error from running my code but I still managed to run the program and get correct output. Can someone explain why the code works but the tester doesn’t work?

“Tests failed to run due to an error: “‘int’ object is not iterable”. Check your code and try again.”

Here’s my code:

def flatten_array(arr):
lst = []
for i in arr:
if len(str(i)) > 1:
for j in i:
lst.append(j)
else:
lst.append(i)
return lst
print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))