Python Challenge - Flatten an Array

Hello, i found a similar approach for my solution :grin:

def flatten_array(arr):

combinedList = []
for j in arr:
   
        if(type(j)==list):
            combinedList.extend((j))
        else:
            combinedList.append(j)

return combinedList

print(flatten_array([1,2,3, [4,5], 6, [7,8], 9]))

only works for 2D list.

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

I don’t know if it matters but I have worked with the array itself. Hope it will be useful!

def flatten_array(arr): for index, item in enumerate(arr): if(type(item) == list): pos = 0 for i in item: arr.insert(index + pos, i) pos += 1 arr.remove(item) return arr print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))

Hello,

This was my solution to this challange

def flatten_array(arr): flatt_arr = [] for i in arr: try: if len(i) > 1: for d in i: flatt_arr.append(d) except TypeError: flatt_arr.append(i) return flatt_arr print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))

I have tried it the following way:
def flatten_array(arr):
i = 0
j = 0
flat =
for i in arr:
if type(i) != list:
flat.append(i)
i = i +1
if type(i) == list:
for j in i:
flat.append(j)
j = j +1

return flat

print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))
sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
print(flatten_array(sales_data))

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

def flatten_array(arr):

Write your code here

arr_flat =
for i in arr:
if type(i) == int:
arr_flat.append(i)
elif type(i) == list:
for j in i:
arr_flat.append(j)
return(arr_flat)

print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))

def flatten_array(arr):
ret=
for subarr in arr:
ty=str(type(subarr))
if (ty.find(“list”)>0):
for subelm in subarr:
ret.append(subelm)
else:
ret.append(subarr)
return ret

print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))

def flatten_array(arr): nlist = [] for a in arr: if type(a) == list: nlist += [i for i in a] else: nlist.append(a) return nlist print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))
def flatten_array(arr): new_arr = [] getElements(arr, new_arr) return new_arr def getElements(arr, resultArray): for element in arr: if type(element) == type([]): getElements(element, resultArray) else: resultArray.append(element) print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))
def flatten_array(arr): new_array = [] for x in arr: if type(x) is int: new_array.append(x) else: for i in x: new_array.append(i) return new_array print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))
def flatten_array(arr): # Write your code here lst = [] for item in arr: if type(item) == list: lst += item else: lst.append(item) return lst print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))
def flatten_array(arr): # Write your code here for el in arr: if isinstance(el,list): idx = arr.index(el) arr.pop(idx) for i in range(len(el)): arr.insert(idx+i,el[i]) return arr print(flatten_array([1, 2, [3, 4, 5], 6, [7, 8], 9]))