18. Using a list of lists in a function


#1



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


i just want to know why do we use double for loops to break 2 arrays within an array


Replace this line with your code. 
n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    result=[]
    for x in lists:
        for i in x:
         result.append(i)
    return result    



print flatten(n)


#2

well, the first loop gives us access to the inner list:

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



print flatten(n)

then we need a second loop to get the values from the inner list, so you can append them to a new list (flatten the list)


#3

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