Extracting elements from lists of lists


#1



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):
    results=[]
    for numbers in lists:
        results.append(numbers)
    return results

print flatten(n)

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


#2

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.

or

using extend() method.


#3

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