repeat items in a list

Paul Rubin no.email at nospam.invalid
Sun Mar 27 01:35:28 EDT 2016


beliavsky at aol.com writes:
> yy = list(chain.from_iterable([list(repeat(aa,nrep)) for aa in xx]))

The chain approach seems more natural to me:

  yy = list(chain.from_iterable(map(lambda x: [x,x], xx)))

may make the doubling more obvious, and in Python 3 it should avoid the
intermediate lists since map creates a generator.  Depending on the
usage, you might also not need the outermost list, making yy a lazy
generator as well.



More information about the Python-list mailing list