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. -- https://mail.python.org/mailman/listinfo/python-list