18. using a list of lists in a function



Oops, try again. flatten([[1, 2], [3, 4]]) returned [1] instead of [1, 2, 3, 4]

It prints


I tried replacing number with numbers in results.append(number)
Can't see any other errors..

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

print flatten(n)


n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten(lists):
    results = []
    for numbers in lists:
        for a in range (0, len(lists)):
            results = results + lists[a] 
        return results 

print flatten(n)

'for a in range (0, len(lists))'
This says for variable a in the length from zero to the length of your list plus a variable results is equal to itself. I believe you can simplify this code even further and say
results += lists[a] (adds results to lists[a] and makes it equal to itself). But
results = results + lists[a] is... DA DA DA DAAAA! STRING CONCATENATION!! :slight_smile:

You may want to more carefully read the instructions:

'Create a function called flatten that takes a single list and concatenates all the sublists that are part of it into a single list'

Here is a link on string concatenation:


So your saying

  1. use for numbers in range (0, len(list)): and results += lists[a] to add both list together [1,2,3,4,5,6,7,etc.]

I still don't get why the steps tell you to use .append? all append does is add whatever u want in the bracket to the list according to my understanding.

My guess is you can still complete the code with append, but your way results += lists[a] is more easier to understand or simplier??


You are correct, that is what append does. You can do the code with .append(), however I don't really understand why it's necessary when you can make it simpler. Simple code is beautiful code :slight_smile:

By storing the concatenation of the lists in the variable results, all we really need to do is return result.


I just get a bit confused as the steps tell u something and, i'm told that this other way is the right way..

so theirs just multiple wasy to solve the problem :smile:


There are most always multiple ways to solve the problem. My guess is that codecademy wants to make you utilize the append function, but if you can do it a simpler way, why not?


oh alright, I'll try and solve this problem both ways then! :grin: Double the fun! :rofl:


I'm a bit confused on how you said that for a in range (0, len(lists)) "plus a variable result is equal to itself..."


This is referring to this code bit below:

results = results + lists[a] 
        return results

results += lists[a]


Oh okay, Yeah I was confused on how for a in range(0, len(lists)): does plus a variable result is equal to itself :rofl:


What exactly does range(0, len(lists)): . do?


range(0, len(lists)) just sets the range of indexes that you are using. range(start, stop) -> the start in this case is index 0 (the very first thing), and then the stop is len(lists), which is the length of the list (up until and including the last index)


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