repeat items in a list

Michael Selik michael.selik at gmail.com
Tue Mar 29 18:05:16 EDT 2016


I prefer itertools.chain.from_iterable to the sum trick.

>>> from itertools import chain
>>> lst = list('abc')
>>> list(chain.from_iterable([s]*3 for s in lst))
['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']



On Tue, Mar 29, 2016 at 5:28 PM Vito De Tullio <vito.detullio at gmail.com>
wrote:

> Random832 wrote:
>
> > How do you turn ['a', 'c', 'b'] into ['a', 'a', 'a', 'c', 'c', 'c', 'b',
> > 'b', 'b']?
>
> >>> sum([[e]*3 for e in ['a', 'c', 'b']], [])
> ['a', 'a', 'a', 'c', 'c', 'c', 'b', 'b', 'b']
>
>
> --
> By ZeD
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list