repeat items in a list
Antonio Caminero Garcia
tonycamgar at gmail.com
Sun Mar 27 07:13:54 EDT 2016
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.
More information about the Python-list
mailing list