Lists & Functions, #18, Explanation of what's happening


#1



I have the answer, i just want to make sure I'm understanding what's going on.

-list of 2 lists.
-define function.
-create empty list called "results".
-take the brackets off the lists, essentially creating a chunk of raw data.
-take each piece of that raw data,
-add it to the originally empty list "results".
-return the completed list.

Right?!?

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

def flatten(lists):
    results=[]
    for numbers in range(len(lists)):
        for item in lists[numbers]:
            results.append(item)
    return results

print flatten(n)


#2

we call this a multi-dimensional list, but yes, you are right. In this case a 2d list since it has 2 dimensions. 1d list:

a = [1,2,3,4,5,6]

2d:

a = [[1,2,3],[4,5,6]]

yes, so far so good.

not really? you use the first for loop to access the first dimension of the list. (why use range? it complicates things), then you use the second loop to access the second dimension, and append the items of the second dimension into a new list, creating a flattened list (only 1 dimensional)


#3

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