18/18 Using a list of lists in a function


#1


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

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

the output should be [1,2,3,4,5,6,7,8,9] instead of [[1,2,3,4],[5,6,7,8,9]]


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


#2

Currently your code just appending the list elements which are lists only.

To achieve that desired result we have few options..
List concatenation

or

You can use extend() method
or
can introduce another loop to loop over items of child list of main list then append each child-list items to results


#3

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

Add your function here

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

return results

print flatten(n)

please help me understand my mistake


#4

Your code has all of the right pieces just a small mistake in the logic.

There are nested 'for-loops' taking place. You're iterating through each item in the list (which is a list itself). So for each sublist in our list, you want to iterate through the items in that sublist. So in your code if you indent "for i in numbers:", that should do it!


#5

Hello @human1247,

The bug is inside this line of code.

results = results.append([i])

First thing to note, when we put something inside a [] ,It simply means we are creating list with one item, here we only need to append i not [i].

Second point is related to methods defined on objects (like lists,strings etc)
Important thing to understand that some method changes an object on the fly but does not return anything (when a function does not return anything explicitly It returns None)

.append() method changes list object (by adding a new number,list size get increased and so on) on fly but it returns None so if you save it somewhere you're simply saving None in some variable.

so you do not need to save it in some variable,

results.append(item) is suffice.


#6

So
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
''' Add your function here'''

     def flatten(lists):
         result= []
         for number in lists:

''' here you need a second loop to go into each list item (n1 and n2 in n0)'''

             for numbers in number:

''' now you iterate through each number in one list (n1(n1n2n3) then n2(n1n2n3)'''

                result.append(numbers) # here you just add for every iteration one number item to result
          return result

''' now here you need to be careful about the position of the return, because otherwise the return itself will only work for the second loop'''

     print flatten(n)

'''hope now you can understand it a little bit better'''


#7

thanx, I applied the last option and succeded


#8

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

return results


#9

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