18 Using a list of lists


#1

Help guys! I really don't know what's wrong with my code

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]

Add your function here

def flatten(lists):
results = []
for lst in lists:
for numbers in lst:
results = results.append(numbers)
return results

print flatten(n)


#2

Usually one would start with establishing what it's doing differently from what you want

And if you're stuck somewhere, what is it that is stopping you?


#4

Few things which you can do and see if it works-
1) The iterating variable for the main list is numbers so instead of using lst , you can use numbers and then use lst for iterating in numbers

2) The append function doesn't give any return so it should not be equated to result. Instead you can simply write
results.append(lst)
where lst is your iterating variable inside the numbers.

See if it works and let us know


#5

thank u very much! when I remove "results=", the code works well


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.