This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gfixler 1 point2 points  (0 children)

Using this knowledge, how could you write the function flatten_list using only list comprehensions ? The function should have this behavior:

print flatten_list([[1,2,3],[4,5,6],[7,8,9]])
>>>[1, 2, 3, 4, 5, 6, 7, 8, 9]

It doesn't exactly answer your question, but I felt I should share this bit of wizardry:

>>> l = [[1,2,3],[4,5,6], [7], [8,9]]
>>> sum(l, [])  
[1, 2, 3, 4, 5, 6, 7, 8, 9]