18. Using a list of lists in a function


#1




Oops, try again. Your code looks a bit off. Check the console window for errors!

i think the iteration statement is correct but after there is something wrong

Replace this line with your code. 

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


print flatten(n)


#2

.append() is a built-in function to append to a list, you are currently trying to append to a integer


#3

how do i append to a integer?


#4

uhm... your need to make a flattened list? this means the result should be a list, not a integer. you increase an integer with +, but that is not what you want to do in this exercise


#5

what is a flattened list?
and you mentioned to increase the integer with + , i didnt understand that part. how do i do it? i am sorry, i am really confused


#6

ok i understand what flattened list is.
how do i finish this excercise?


#7

this list:

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

is at the moment multi-dimensional (2 dimensional, 2d), it has a list within the list another list. You want to flatten the list (make it 1d), so then you should get:

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

so you will need two loops to get the values of the inner list which you can append to results (results should be a list, not a integer)


#8

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


print flatten(n)

can you check now?


#9


#10

you want to have a list in the end, so results should be a empty list at the start to which you can append the numbers. result is a integer in your case


#11

super blunder mistake. i get it now. sorry for the trouble!


#12

in your case @shrijan23 your have declared an empty variable ,"results=0", but we have declare an empty list ie; "results=[]"........... try this it will work..


#14

shrijan23 already indicated he got it to work


#15