Extracting elements from lists of lists


12. Lists and functions 18. lists of lists

Trying to get it to extract the elements of the nested lists to append them individually to results, but I can only get them to pull as lists.

Red box error message:flatten([[1, 2], [3, 4]]) returned [[1, 2], [3, 4]] instead of [1, 2, 3, 4]

getting [[1,2,3],[4,5,6]]
should get [1,2,3,4,5,6]

n = [[1, 2, 3], [4, 5, 6, 7, 8, 9]]
# Add your function here
def flatten(lists):
    for numbers in lists:
    return results

print flatten(n)



Your first loop is going through items of lists list,items of lists consists of lists only.
Conventional way to solve this is to add one more loop so it loop over each item of child lists.

Few other options.
use of + operator.


using extend() method.


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