Re: repeat items in a list

2016-03-30 Thread Peter Otten
Michael Selik wrote: > 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'] If you want to make this completely lazy: >>> from functoo

Re: repeat items in a list

2016-03-30 Thread Michael Selik
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 wrote: > Random832 wrote: > > > How do

Re: repeat items in a list

2016-03-29 Thread Vito De Tullio
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

Re: repeat items in a list

2016-03-29 Thread Antonio Caminero Garcia
On Monday, March 28, 2016 at 11:26:08 PM UTC+2, Chris Angelico wrote: > On Tue, Mar 29, 2016 at 4:30 AM, Rob Gaddi > wrote: > > beliav...@aol.com wrote: > > > >> On Saturday, March 26, 2016 at 7:24:10 PM UTC-4, Erik wrote: > >>> > >>> Or, if you want to "import operator" first, you can use 'operat

Re: repeat items in a list

2016-03-28 Thread Erik
On 28/03/16 22:25, Chris Angelico wrote: Just out of interest, did you (generic you) happen to notice Mark's suggestion? It's a one-liner that nicely expresses the intention and accomplishes the goal: yy = [aa for aa in xx for _ in range(nrep)] It quietly went through without fanfare, but I wo

Re: repeat items in a list

2016-03-28 Thread Chris Angelico
On Tue, Mar 29, 2016 at 4:30 AM, Rob Gaddi wrote: > beliav...@aol.com wrote: > >> On Saturday, March 26, 2016 at 7:24:10 PM UTC-4, Erik wrote: >>> >>> Or, if you want to "import operator" first, you can use 'operator.add' >>> instead of the lambda (but you _did_ ask for a one-liner ;)). >>> >>> Ou

Re: repeat items in a list

2016-03-28 Thread Rob Gaddi
beliav...@aol.com wrote: > On Saturday, March 26, 2016 at 7:24:10 PM UTC-4, Erik wrote: >> >> Or, if you want to "import operator" first, you can use 'operator.add' >> instead of the lambda (but you _did_ ask for a one-liner ;)). >> >> Out of interest, why the fascination with one-liners? > > T

Re: repeat items in a list

2016-03-28 Thread Random832
On Mon, Mar 28, 2016, at 07:36, larudwer wrote: > in case you want to mainain order: > > ["a","b"]*3 > ['a', 'b', 'a', 'b', 'a', 'b'] > > is completely suffincient. I think you've completely missed the point of what order he's talking about. How do you turn ['a', 'c', 'b'] into ['a', 'a',

Re: repeat items in a list

2016-03-28 Thread larudwer
Am 27.03.2016 um 13:13 schrieb Antonio Caminero Garcia: On Sunday, March 27, 2016 at 11:52:22 AM UTC+2, larudwer wrote: how about sorted(["a", "b"]*3) ['a', 'a', 'a', 'b', 'b', 'b'] that's cooler, less efficient though and do not maintain the original order. In case such order was impo

Re: repeat items in a list

2016-03-27 Thread Antonio Caminero Garcia
On Sunday, March 27, 2016 at 11:52:22 AM UTC+2, larudwer wrote: > how about > > sorted(["a", "b"]*3) > ['a', 'a', 'a', 'b', 'b', 'b'] that's cooler, less efficient though and do not maintain the original order. In case such order was important, you should proceed as follows: If the elemen

Re: repeat items in a list

2016-03-27 Thread Jussi Piitulainen
Antonio Caminero Garcia writes: > On Saturday, March 26, 2016 at 11:12:58 PM UTC+1, beli...@aol.com wrote: >> I can create a list that has repeated elements of another list as >> follows: >> >> xx = ["a","b"] >> nrep = 3 >> print xx >> yy = [] >> for aa in xx: >> for i in range(nrep): >>

Re: repeat items in a list

2016-03-27 Thread larudwer
how about sorted(["a", "b"]*3) ['a', 'a', 'a', 'b', 'b', 'b'] -- https://mail.python.org/mailman/listinfo/python-list

Re: repeat items in a list

2016-03-27 Thread Antonio Caminero Garcia
On Sunday, March 27, 2016 at 10:02:44 AM UTC+2, Antonio Caminero Garcia wrote: > On Saturday, March 26, 2016 at 11:12:58 PM UTC+1, beli...@aol.com wrote: > > I can create a list that has repeated elements of another list as follows: > > > > xx = ["a","b"] > > nrep = 3 > > print xx > > yy = [] > >

Re: repeat items in a list

2016-03-27 Thread Antonio Caminero Garcia
On Saturday, March 26, 2016 at 11:12:58 PM UTC+1, beli...@aol.com wrote: > I can create a list that has repeated elements of another list as follows: > > xx = ["a","b"] > nrep = 3 > print xx > yy = [] > for aa in xx: > for i in range(nrep): > yy.append(aa) > print yy > > output: > ['a

Re: repeat items in a list

2016-03-26 Thread Paul Rubin
beliav...@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 sinc

Re: repeat items in a list

2016-03-26 Thread beliavsky--- via Python-list
On Saturday, March 26, 2016 at 7:30:14 PM UTC-4, Mark Lawrence wrote: > On 26/03/2016 22:12, beliavsky--- via Python-list wrote: > > I can create a list that has repeated elements of another list as follows: > > > > xx = ["a","b"] > > nrep = 3 > > print xx > > yy = [] > > for aa in xx: > > for

Re: repeat items in a list

2016-03-26 Thread beliavsky--- via Python-list
On Saturday, March 26, 2016 at 7:24:10 PM UTC-4, Erik wrote: > Hi, > > On 26/03/16 22:12, beliavsky--- via Python-list wrote: > > I can create a list that has repeated elements of another list as follows: > > > > xx = ["a","b"] > > nrep = 3 > > print xx > > yy = [] > > for aa in xx: > > for i

Re: repeat items in a list

2016-03-26 Thread Mark Lawrence
On 26/03/2016 22:12, beliavsky--- via Python-list wrote: I can create a list that has repeated elements of another list as follows: xx = ["a","b"] nrep = 3 print xx yy = [] for aa in xx: for i in range(nrep): yy.append(aa) print yy output: ['a', 'b'] ['a', 'a', 'a', 'b', 'b', 'b']

Re: repeat items in a list

2016-03-26 Thread Erik
Hi, On 26/03/16 22:12, beliavsky--- via Python-list wrote: I can create a list that has repeated elements of another list as follows: xx = ["a","b"] nrep = 3 print xx yy = [] for aa in xx: for i in range(nrep): yy.append(aa) print yy output: ['a', 'b'] ['a', 'a', 'a', 'b', 'b', '

Re: repeat items in a list

2016-03-26 Thread Cameron Simpson
On 26Mar2016 15:12, beliav...@aol.com wrote: I can create a list that has repeated elements of another list as follows: xx = ["a","b"] nrep = 3 print xx yy = [] for aa in xx: for i in range(nrep): yy.append(aa) print yy output: ['a', 'b'] ['a', 'a', 'a', 'b', 'b', 'b'] Is there a on

repeat items in a list

2016-03-26 Thread beliavsky--- via Python-list
I can create a list that has repeated elements of another list as follows: xx = ["a","b"] nrep = 3 print xx yy = [] for aa in xx: for i in range(nrep): yy.append(aa) print yy output: ['a', 'b'] ['a', 'a', 'a', 'b', 'b', 'b'] Is there a one-liner to create a list with repeated element