18. list of lists inside a function


#1



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


So i have succefully completed the lesson, but i have a question as to why i cant complete in a different way. Why cant i complete the program by using a range instead of the list lists?


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

def flatten(lists):
    results = []
    for numbers in range(0, len(lists)):
        for num in numbers:
            results.append(num)
    return results
print flatten(n)


#2

why does the for loop need to be

instead of


#3

it doesn't, but using range will give you indexes:

example = ['a','b','c','d','e']
for i in range(len(example)):
    print i
    print example[i]
for i in example:
    print i

that should clarify it for you


#4

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