Plz anyone explain this


#1

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten (lists):
    result = []
    for i in range(len(lists)):
        for j in range(len(lists[i])):
            result.append(lists[i][j])
    return result
print flatten(n)

#2

well, you wrote the code, didn't you?

What is it you don't understand? you use two loops, to reach the nested list, and then you append this to the result list, you could add a print statement:

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
def flatten (lists):
    result = []
    for i in range(len(lists)):
        for j in range(len(lists[i])):
            print lists[i][j]
            result.append(lists[i][j])
    return result
print flatten(n)

to see what you are appending, but explaining everything is quit a lot


#3

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