top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Python: Is there a one-liner to create a list with repeated elements?

0 votes
610 views

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?

posted Mar 26, 2016 by Luv Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote

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.

answer Mar 26, 2016 by Sridharan
0 votes

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 initialized with iterables).

answer Mar 26, 2016 by Majula Joshi
0 votes
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 ;)).

answer Mar 26, 2016 by Majula Joshi
Similar Questions
+1 vote

A list can contain different types of elements but I am not able to understand how max () method work with different types of data elements.

+2 votes

Also is there a way to detect if the user presses a key in Python that works on most OS's? I've only seen 1 method and that only works in Python 2.6 and less. If you get the key, can you store it in a variable?

0 votes

We have few rules (changeable) are stored in a database, and I need to display them for the end user in the form of flow-chart.

Is there such thing for Python?

+3 votes

Is there way to get list of instances of particular class through class itself? via metaclass or any other method?

If class is object is it possible to delete it? If it is possible then how instances of that class will behave?

+2 votes

How can I flatten just a specific sublist of each list in a list of lists?

So if I had this data

[ ['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 $277790.00']],
 ['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 $105625.00']],
 ['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 $71685.00']],
 ['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 $112645.00']]]

How can I make it be

[ ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00'],
 ['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00'],
 ['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 $71685.00'],
 ['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']]

Been looking around but most solutions just entirely flatten everything. This was popular on SO but yeah it flattens everything I want to be more selective

def flatten(lst):
 for elem in lst:
 if type(elem) in (tuple, list):
 for i in flatten(elem):
 yield i
 else:
 yield elem

What I am thinking is that if for each list the sublist should be at index 1, so

[0][1]
[1][1]
[2][1]

for item in list:
 item[1] - somehow flatten.

Any Idea or pointer?

...