(E-Mail Removed) wrote:
> a list like L = [[1, 2], [3, 4, 5], [6]], which has
> random numbers of list, which has random numbers.
>
> How do I get another list m = [1, 2, 3, 4, 5, 6] in a succinct way? ( or
> iterate them) I should provide my own solution, but I really can't come
> out with one.
sum(L, [])
list(itertools.chain.from_iterable(L))
but as a learning experience you should do this at least once with two
nested for loops, then again with a single for loop and list.extend().
> a list like L = [[1, 2], [3, 4, 5], [6]], which has
> random numbers of list, which has random numbers.
>
> How do I get another list m = [1, 2, 3, 4, 5, 6] in a succinct way? ( or
> iterate them) I should provide my own solution, but I really can't come
> out with one.
sum(L, [])
list(itertools.chain.from_iterable(L))
but as a learning experience you should do this at least once with two
nested for loops, then again with a single for loop and list.extend().
Aucun commentaire:
Enregistrer un commentaire