Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #105787 > unrolled thread

repeat items in a list

Started bybeliavsky@aol.com
First post2016-03-26 15:12 -0700
Last post2016-03-30 17:52 +0200
Articles 20 on this page of 21 — 14 participants

Back to article view | Back to comp.lang.python


Contents

  repeat items in a list beliavsky@aol.com - 2016-03-26 15:12 -0700
    Re: repeat items in a list Cameron Simpson <cs@zip.com.au> - 2016-03-27 10:05 +1100
    Re: repeat items in a list Erik <python@lucidity.plus.com> - 2016-03-26 23:23 +0000
      Re: repeat items in a list beliavsky@aol.com - 2016-03-26 16:36 -0700
        Re: repeat items in a list Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-03-28 17:30 +0000
          Re: repeat items in a list Chris Angelico <rosuav@gmail.com> - 2016-03-29 08:25 +1100
            Re: repeat items in a list Antonio Caminero Garcia <tonycamgar@gmail.com> - 2016-03-29 01:43 -0700
          Re: repeat items in a list Erik <python@lucidity.plus.com> - 2016-03-28 23:14 +0100
    Re: repeat items in a list Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-26 23:28 +0000
      Re: repeat items in a list beliavsky@aol.com - 2016-03-26 16:54 -0700
        Re: repeat items in a list Paul Rubin <no.email@nospam.invalid> - 2016-03-26 22:35 -0700
    Re: repeat items in a list Antonio Caminero Garcia <tonycamgar@gmail.com> - 2016-03-27 01:02 -0700
      Re: repeat items in a list Antonio Caminero Garcia <tonycamgar@gmail.com> - 2016-03-27 01:30 -0700
      Re: repeat items in a list Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-03-27 14:09 +0300
    Re: repeat items in a list larudwer <larudwer@freenet.de> - 2016-03-27 11:52 +0200
      Re: repeat items in a list Antonio Caminero Garcia <tonycamgar@gmail.com> - 2016-03-27 04:13 -0700
        Re: repeat items in a list larudwer <larudwer@freenet.de> - 2016-03-28 13:36 +0200
          Re: repeat items in a list Random832 <random832@fastmail.com> - 2016-03-28 10:04 -0400
          Re: repeat items in a list Vito De Tullio <vito.detullio@gmail.com> - 2016-03-29 23:25 +0200
          Re: repeat items in a list Michael Selik <michael.selik@gmail.com> - 2016-03-29 22:05 +0000
          Re: repeat items in a list Peter Otten <__peter__@web.de> - 2016-03-30 17:52 +0200

Page 1 of 2  [1] 2  Next page →


#105787 — repeat items in a list

Frombeliavsky@aol.com
Date2016-03-26 15:12 -0700
Subjectrepeat items in a list
Message-ID<8935d5dc-5e62-4fa8-8e8f-bd5b1787ee9f@googlegroups.com>
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 elements?

[toc] | [next] | [standalone]


#105793

FromCameron Simpson <cs@zip.com.au>
Date2016-03-27 10:05 +1100
Message-ID<mailman.63.1459033927.28225.python-list@python.org>
In reply to#105787
On 26Mar2016 15:12, beliavsky@aol.com <beliavsky@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 one-liner to create a list with repeated elements?

Sure. As with all one liners, there comes a degree of complexity when it gets 
in the way of readability; you must decide what is better in your use case.

Look up the chain() function from the itertools module. Generate 2 (or nrep) 
length lists from each element of the original list and chain() them together.  
That gets you an iterable of all the elements. If you really need a list out 
the end instead of the iterable of the elements, convert the iterable to a list 
(hint: lists can be initialised with iterables).

Cheers,
Cameron Simpson <cs@zip.com.au>

[toc] | [prev] | [next] | [standalone]


#105795

FromErik <python@lucidity.plus.com>
Date2016-03-26 23:23 +0000
Message-ID<mailman.65.1459034636.28225.python-list@python.org>
In reply to#105787
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', 'b']
>
> Is there a one-liner to create a list with repeated elements?

yy = reduce(lambda a, b: a + b, ([i] * nrep for i in xx), [])

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?

E.

[toc] | [prev] | [next] | [standalone]


#105797

Frombeliavsky@aol.com
Date2016-03-26 16:36 -0700
Message-ID<c4cf6ed9-cf6a-474b-b471-ce1066fd423d@googlegroups.com>
In reply to#105795
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 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 elements?
> 
> yy = reduce(lambda a, b: a + b, ([i] * nrep for i in xx), [])
> 
> 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?

Thanks for your reply. Sometimes when I program in Python I think I am not using the full capabilities of the language, so I want to know if there are
more concise ways of doing things.

[toc] | [prev] | [next] | [standalone]


#105915

FromRob Gaddi <rgaddi@highlandtechnology.invalid>
Date2016-03-28 17:30 +0000
Message-ID<ndbpoc$o2$1@dont-email.me>
In reply to#105797
beliavsky@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?
>
> Thanks for your reply. Sometimes when I program in Python I think I am not using the full capabilities of the language, so I want to know if there are
> more concise ways of doing things.

Concise is only worth so much.  PEP20 tells us "Explicit is better than
implicit", "Simple is better than complex" and "If the implementation is
hard to explain, it's a bad idea".

Python is a beautifully expressive language.  Your goal should not be to
write the minimum number of lines of code to accomplish the task. 
Your goal should be to write the code such that your grandmother can
understand it.  That way, when you screw it up, you'll be able to easily
figure out where and how you did so.  Or failing that, you can get
grangran to show you.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com

Email address domain is currently out of order.  See above to fix.

[toc] | [prev] | [next] | [standalone]


#105925

FromChris Angelico <rosuav@gmail.com>
Date2016-03-29 08:25 +1100
Message-ID<mailman.115.1459200353.28225.python-list@python.org>
In reply to#105915
On Tue, Mar 29, 2016 at 4:30 AM, Rob Gaddi
<rgaddi@highlandtechnology.invalid> wrote:
> beliavsky@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?
>>
>> Thanks for your reply. Sometimes when I program in Python I think I am not using the full capabilities of the language, so I want to know if there are
>> more concise ways of doing things.
>
> Concise is only worth so much.  PEP20 tells us "Explicit is better than
> implicit", "Simple is better than complex" and "If the implementation is
> hard to explain, it's a bad idea".
>
> Python is a beautifully expressive language.  Your goal should not be to
> write the minimum number of lines of code to accomplish the task.
> Your goal should be to write the code such that your grandmother can
> understand it.  That way, when you screw it up, you'll be able to easily
> figure out where and how you did so.  Or failing that, you can get
> grangran to show you.

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 would say this is the
perfect solution to the original problem.

ChrisA

[toc] | [prev] | [next] | [standalone]


#105963

FromAntonio Caminero Garcia <tonycamgar@gmail.com>
Date2016-03-29 01:43 -0700
Message-ID<b2e83f42-03d7-45c9-b1ad-0fad4307df47@googlegroups.com>
In reply to#105925
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
> <rgaddi@highlandtechnology.invalid> wrote:
> > beliavsky@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?
> >>
> >> Thanks for your reply. Sometimes when I program in Python I think I am not using the full capabilities of the language, so I want to know if there are
> >> more concise ways of doing things.
> >
> > Concise is only worth so much.  PEP20 tells us "Explicit is better than
> > implicit", "Simple is better than complex" and "If the implementation is
> > hard to explain, it's a bad idea".
> >
> > Python is a beautifully expressive language.  Your goal should not be to
> > write the minimum number of lines of code to accomplish the task.
> > Your goal should be to write the code such that your grandmother can
> > understand it.  That way, when you screw it up, you'll be able to easily
> > figure out where and how you did so.  Or failing that, you can get
> > grangran to show you.
> 
> 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 would say this is the
> perfect solution to the original problem.
> 
> ChrisA

Of course that's definitely the most pythonic sol to this prob :)! Just wanted to point out the use of the operator "*" in lists.

[toc] | [prev] | [next] | [standalone]


#105929

FromErik <python@lucidity.plus.com>
Date2016-03-28 23:14 +0100
Message-ID<mailman.118.1459203257.28225.python-list@python.org>
In reply to#105915
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 would say this is the
> perfect solution to the original problem.

I noticed it (and I timed it - it was ~30% faster than my version 
(because mine was creating short transient list objects), but it takes a 
_LOT_ - millions - of iterations of the example case (nrep = 3, xx = 
two-element-list) to even make it measurable on my PC). It would have 
probably been even quicker if he'd cached the range() object.

I'm not convinced it's particularly intuitive, though. That trailing 
"for _ in range(nrep)" looks at first glance like an error - some code 
that's generating a value that is not referenced anywhere else.

It makes perfect sense when one analyses it, but it's not the most 
immediately grokkable construct.

Hmmm. It's almost as if in this instance I'd prefer something like:

   yy = [for aa in xx for _ in range(nrep): aa]

But I know we can't go /there/ ;)

E.

[toc] | [prev] | [next] | [standalone]


#105796

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2016-03-26 23:28 +0000
Message-ID<mailman.66.1459034995.28225.python-list@python.org>
In reply to#105787
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']
>
> Is there a one-liner to create a list with repeated elements?
>

yy = [aa for aa in xx for _ in range(nrep)]

I suggest that you try this sort of the thing at an interactive prompt, 
it's a great way to learn.

You might also want to take a look at the itertools module 
https://docs.python.org/3/library/itertools.html.  This is often used in 
building structures like the ones you've been asking about today.  To me 
it is the Swiss Army Knife of the stdlib.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#105798

Frombeliavsky@aol.com
Date2016-03-26 16:54 -0700
Message-ID<6f1ab11b-da4a-49d5-8dcd-bd9b0c6fe8b3@googlegroups.com>
In reply to#105796
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 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 elements?
> >
> 
> yy = [aa for aa in xx for _ in range(nrep)]
> 
> I suggest that you try this sort of the thing at an interactive prompt, 
> it's a great way to learn.
> 
> You might also want to take a look at the itertools module 
> https://docs.python.org/3/library/itertools.html.  This is often used in 
> building structures like the ones you've been asking about today.  To me 
> it is the Swiss Army Knife of the stdlib.

Thanks for the one-liner, which I prefer to the one I made up using itertools:

yy = list(chain.from_iterable([list(repeat(aa,nrep)) for aa in xx]))

[toc] | [prev] | [next] | [standalone]


#105816

FromPaul Rubin <no.email@nospam.invalid>
Date2016-03-26 22:35 -0700
Message-ID<8737rc7aan.fsf@nightsong.com>
In reply to#105798
beliavsky@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.

[toc] | [prev] | [next] | [standalone]


#105828

FromAntonio Caminero Garcia <tonycamgar@gmail.com>
Date2016-03-27 01:02 -0700
Message-ID<74fc27bf-22fe-4f7e-ac54-78be6b518562@googlegroups.com>
In reply to#105787
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', 'b']
> ['a', 'a', 'a', 'b', 'b', 'b']
> 
> Is there a one-liner to create a list with repeated elements?

What about this?

def rep_elements(sequence, nrep):
    #return [ritem for item in sequence for ritem in [item]*nrep]
    return list(chain.from_iterable(([item]*nrep for item in sequence)))

sequence = ['h','o','l','a']
print(rep_elements(sequence,  3))

[toc] | [prev] | [next] | [standalone]


#105829

FromAntonio Caminero Garcia <tonycamgar@gmail.com>
Date2016-03-27 01:30 -0700
Message-ID<20935e0a-0b54-4213-b651-b33ad3910510@googlegroups.com>
In reply to#105828
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 = []
> > 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 elements?
> 
> What about this?
> 
> def rep_elements(sequence, nrep):
>     #return [ritem for item in sequence for ritem in [item]*nrep]
>     return list(chain.from_iterable(([item]*nrep for item in sequence)))
> 
> sequence = ['h','o','l','a']
> print(rep_elements(sequence,  3))

I prefer the commented solution :).

[ritem for item in sequence for ritem in [item]*nrep] # O(len(sequence)*2nrep) 

and the chain solution  would be # O(len(sequence)*nrep). The constants ate gone so I prefer the first one for its readibility.

On a practical level:

https://bpaste.net/show/fe3431a13732

[toc] | [prev] | [next] | [standalone]


#105833

FromJussi Piitulainen <jussi.piitulainen@helsinki.fi>
Date2016-03-27 14:09 +0300
Message-ID<lf5bn60qis8.fsf@ling.helsinki.fi>
In reply to#105828
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):
>>         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 elements?
>
> What about this?
>
> def rep_elements(sequence, nrep):
>     #return [ritem for item in sequence for ritem in [item]*nrep]
>     return list(chain.from_iterable(([item]*nrep for item in sequence)))
>
> sequence = ['h','o','l','a']
> print(rep_elements(sequence,  3))

A thing to know about the comprehension-syntaxes is that they correspond
precisely to nested loops (and conditions, but conditions don't appear
in the present example) with an .append inside.

xx = "ab"
nrep = 3
print([ aa for aa in xx for i in range(nrep) ])

(This has been posted in this thread a few times already, but I think
the systematic correspondence to the original loops was left unstated.
Apologies in advance if I missed something.)

The resulting list has some hidden name. The original loops should be
re-mentalized for an even closer correspondence as follows.

g47 = []
for aa in xx:            # Loopy ...
   for i in range(nrep): # ... do!
      g47.append(aa)     # <-- _This_ aa is one of the result items.
yy = g47

A thing about range objects is that they can be reused, so the present
example could also reuse just one.

xx = "ab"
reps = range(3)
print([ aa for aa in xx for i in reps ])

[toc] | [prev] | [next] | [standalone]


#105831

Fromlarudwer <larudwer@freenet.de>
Date2016-03-27 11:52 +0200
Message-ID<nd8ag6$6gh$1@gioia.aioe.org>
In reply to#105787
how about

 >>>> sorted(["a", "b"]*3)
['a', 'a', 'a', 'b', 'b', 'b']

[toc] | [prev] | [next] | [standalone]


#105834

FromAntonio Caminero Garcia <tonycamgar@gmail.com>
Date2016-03-27 04:13 -0700
Message-ID<29bb9c16-eef4-4c00-9e41-ce8419405c29@googlegroups.com>
In reply to#105831
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 elements are unique, this would work:

sorted(sequence*nrep, key=sequence.index)

Otherwise you'd need a more complex key function (maybe a method of a class with a static variable that tracks the number of times that such method is called and with a "dynamic index functionality" that acts accordingly (i-th nrep-group of value v)) and imo it does not worth it.

[toc] | [prev] | [next] | [standalone]


#105899

Fromlarudwer <larudwer@freenet.de>
Date2016-03-28 13:36 +0200
Message-ID<ndb50q$b8e$2@gioia.aioe.org>
In reply to#105834
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 important, you should proceed as follows:
>
> If the elements are unique, this would work:
>
> sorted(sequence*nrep, key=sequence.index)
>
> Otherwise you'd need a more complex key function (maybe a method of a class with
 > a static variable that tracks the number of times that such method is 
called and
 > with a "dynamic index functionality" that acts accordingly (i-th 
nrep-group of value v))
 > and imo it does not worth it.
>


in case you want to mainain order:

 >>>> ["a","b"]*3
['a', 'b', 'a', 'b', 'a', 'b']

is completely suffincient.

[toc] | [prev] | [next] | [standalone]


#105901

FromRandom832 <random832@fastmail.com>
Date2016-03-28 10:04 -0400
Message-ID<mailman.105.1459173886.28225.python-list@python.org>
In reply to#105899
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', 'a', 'c', 'c',
'c', 'b', 'b', 'b']?

[toc] | [prev] | [next] | [standalone]


#106002

FromVito De Tullio <vito.detullio@gmail.com>
Date2016-03-29 23:25 +0200
Message-ID<mailman.167.1459286749.28225.python-list@python.org>
In reply to#105899
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

[toc] | [prev] | [next] | [standalone]


#106061

FromMichael Selik <michael.selik@gmail.com>
Date2016-03-29 22:05 +0000
Message-ID<mailman.198.1459342300.28225.python-list@python.org>
In reply to#105899
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@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
>

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.python


csiph-web