18. using a list of lists in a function


#1



https://www.codecademy.com/en/courses/python-beginner-nzzVa/4/2?curriculum_id=4f89dab3d788890003000096#


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


It prints

[1]
None

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:
            results.append(number)
            return results
    



print flatten(n)


#2

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:
https://docs.python.org/2/library/string.html


#3

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??


#4

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.


#5

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:


#6

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?


#7

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


#8

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..."


#9

This is referring to this code bit below:

results = results + lists[a] 
        return results

results += lists[a]


#10

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:


#11

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


#12

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)


#13

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