Using a list of lists in a function. (I know its new years day but I think I'm quite close)


On this exercise it keeps showing me this:
[File "python", line 7, in flatten

basically saying lists.append(number) is wrong but I have no idea how to correct this. Everything else looks fine to me but isn't line 7 of code supposed to join the two lists together? I am appending the lists made up of numbers. I have spent hours and have gotten nowhere, resorting to trial and error. The hints are desperately vague.

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    results = ""
    for lists in n:
        for item in lists:
    return results
print flatten(n)


number isn't defined anywhere, so what is it appending?


currently your function only works for one list. What if you want the function to also work for a different lists which we pass as argument on function call?

to add to ionation, not only what are you appending, but could you tell us what you are appending to?


tell us what it is appending


@systemace03874 and @ionatan instructions tell me to append each number to results so I've no idea maybe theres a gap in my knowledge.


If so go back and review to see what you missed.:slight_smile:


are you appending to results? If so, where? Show us the where you appending to results and tell us why you think you are appending to results


how do you append to something, ? to append means to join or to add on to something. I want to join the two lists together but how do you code that? both lists of n I want to link into one


well you create an empty lists, then append the items of this two lists to the empty list. This way you have one list containing data from two lists.


@dataplayer34209 go back and review a little and try the exercise again.
Maybe you skipped a lesson?


ok so I have appended n (my two lists) to the empty list which is results. So the code is: n.append(results)
this now returns this error:
Oops, try again. flatten([[1, 2], [3, 4]]) returned '' instead of [1, 2, 3, 4]
What now?


can i see an updated version of your full code? Remember we want our function to work for all lists we pass as argument on function call.

Maybe you should also check append documentation to see how append() works


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

Add your function here

def flatten(lists):
results = ""
for lists in n:
for item in lists:
return results

print flatten(n)


all indentations are correct thought they aren't showed there


so currently you are appending an empty string to list which you want to join together? tutorialspoint has a simple explanation of append:

Maybe you should read that for a second

as for your indent, read this topic:


I still don't understand and I can't see what I'm doing wrong. Can I see the correct code so that I can backtrack and see where I went wrong? Thats how I work best. Thanks


no, this is the easiest approach but doesn't teach you to think in steps on how to tackle a problem and break it down.

Can i see an updated version of your code?


You need to start with deciding what actions your program should be carrying out.

You're trying to write code before deciding what it should do. That it doesn't work out is not surprising.

So, if you have a list of lists of values, and you want a list with all those values, what actions would have that effect? Write it out in English, if you like you can write them as comments. Keep in mind that each thing you write has to be something you know how to do in code, so if something is complicated then you have to describe it in greater detail. You can keep it in your head as well, or write the code directly. But you do need to consider it, you can't go from purpose to code without considering how to achieve that purpose. No complicated actions are required, you need to do some looping and perhaps use a few variables - so lacking knowledge of the language isn't a show stopper.

You also need to keep track of your variables. At the beginning of your function the only thing you have is the argument that was given to the function, so you will have to manipulate that. You can't for example use the global variable n, because that isn't necessarily the argument to the function, and you wouldn't need to look outside the function for that value anyway because the input is given as an argument.


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

Add your function here

def flatten(lists):
results = []
for numbers in lists:
for item in numbers:
return results

print flatten(n)

first, a list should be " name[ ] ", so the result should be results = []

second, the question ask use numbers as a variable for lists, so it should be " for numbers in lists:
for item in numbers. "
The first list is lists and the second list is numbers
append(item), because item is the smallest variable in the final list---numbers
So your code is total wrong....


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