Python 18. Using a list of lists in a function


Hi, everybody. I don't know what's wrong with my code. Could you show me? :slight_smile:

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

print flatten(n)


using + will extends a list, this can be useful:

print [1,2,3]+[4,5,6]

but lists can't be extended with a integer (numbers[i]), which function did we learn to add to a list?


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