763

I want to create a series of lists, all of varying lengths. Each list will contain the same element e, repeated n times (where n = length of the list).

How do I create the lists, without using a list comprehension [e for number in xrange(n)] for each list?

smci
  • 32,567
  • 20
  • 113
  • 146
chimeracoder
  • 20,648
  • 21
  • 60
  • 60

10 Answers10

1126

You can also write:

[e] * n

You should note that if e is for example an empty list you get a list with n references to the same list, not n independent empty lists.

Performance testing

At first glance it seems that repeat is the fastest way to create a list with n identical elements:

>>> timeit.timeit('itertools.repeat(0, 10)', 'import itertools', number = 1000000)
0.37095273281943264
>>> timeit.timeit('[0] * 10', 'import itertools', number = 1000000)
0.5577236771712819

But wait - it's not a fair test...

>>> itertools.repeat(0, 10)
repeat(0, 10)  # Not a list!!!

The function itertools.repeat doesn't actually create the list, it just creates an object that can be used to create a list if you wish! Let's try that again, but converting to a list:

>>> timeit.timeit('list(itertools.repeat(0, 10))', 'import itertools', number = 1000000)
1.7508119747063233

So if you want a list, use [e] * n. If you want to generate the elements lazily, use repeat.

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 40
    It's highly unlikely that the performance of creating a list with identical elements will be a critical component of the performance of a python program. – Arthur Jun 20 '16 at 17:25
  • 23
    As mentioned above, if e is an empty list `[[]] * n` can produce unexpected results . To create [unique empty sub-lists](https://stackoverflow.com/a/7924716/1048186), use for-comprehension: `[[] for i in range(0,n)]` – Josiah Yoder Aug 15 '17 at 17:01
212
>>> [5] * 4
[5, 5, 5, 5]

Be careful when the item being repeated is a list. The list will not be cloned: all the elements will refer to the same list!

>>> x=[5]
>>> y=[x] * 4
>>> y
[[5], [5], [5], [5]]
>>> y[0][0] = 6
>>> y
[[6], [6], [6], [6]]
viebel
  • 19,372
  • 10
  • 49
  • 83
new name
  • 15,861
  • 19
  • 68
  • 114
122

Create List of Single Item Repeated n Times in Python

Depending on your use-case, you want to use different techniques with different semantics.

Multiply a list for Immutable items

For immutable items, like None, bools, ints, floats, strings, tuples, or frozensets, you can do it like this:

[e] * 4

For example:

>>> [None] * 4
[None, None, None, None]

Note that this is usually only used with immutable items (strings, tuples, frozensets, etc) in the list, because they all point to the same item in the same place in memory.

For an example use-case, I use this when I have to build a table with a schema of all strings, so that I don't have to give a highly redundant one to one mapping.

schema = ['string'] * len(columns)

Multiply the list where we want the same item with mutable state repeated

Multiplying a list gives us the same elements over and over. The need for this is infrequent:

[iter(iterable)] * 4

This is sometimes used to map an iterable into a list of lists:

>>> iterable = range(12)
>>> a_list = [iter(iterable)] * 4
>>> [[next(l) for l in a_list] for i in range(3)] # uninteresting usage
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]

We can see that a_list contains the same range iterator four times:

>>> from pprint import pprint
>>> pprint(a_list)
[<range_iterator object at 0x7f9fe3b58420>,
 <range_iterator object at 0x7f9fe3b58420>,
 <range_iterator object at 0x7f9fe3b58420>,
 <range_iterator object at 0x7f9fe3b58420>]

Mutable items

I've used Python for a long time now, and I have seen very few use-cases where I would do the above with mutable objects.

Instead, to have repeated, say, a mutable empty list, set, or dict, you should do something like this:

list_of_lists = [[] for _ in iterator_of_needed_length]

The underscore is simply a throwaway variable name in this context.

If you only have the number, that would be:

list_of_lists = [[] for _ in range(4)]

The _ as the throwaway name is not really special, but a static code analyzer will probably complain if you don't intend to use the variable and use any other name.


Caveats for using the multiplication method with mutable items:

Beware doing this with mutable objects, when you change one of them, they all change because they're all the same object:

foo = [[]] * 4
foo[0].append('x')

foo now returns:

[['x'], ['x'], ['x'], ['x']]

But with immutable objects, you can make it work because you change the reference, not the object:

>>> l = [0] * 4
>>> l[0] += 1
>>> l
[1, 0, 0, 0]

>>> l = [frozenset()] * 4
>>> l[0] |= set('abc')
>>> l
[frozenset(['a', 'c', 'b']), frozenset([]), frozenset([]), frozenset([])]

But again, mutable objects are no good for this, because in-place operations change the object, not the reference:

l = [set()] * 4
>>> l[0] |= set('abc')    
>>> l
[set(['a', 'c', 'b']), set(['a', 'c', 'b']), set(['a', 'c', 'b']), set(['a', 'c', 'b'])]
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
  • The "Multiply the list where we want the same item repeated" section doesn't appear related to the question. It looks like an answer to [How do I split a list into equally-sized chunks?](https://stackoverflow.com/questions/312443/) instead. – Karl Knechtel Aug 26 '23 at 09:44
  • It illustrates one of the use cases for the question of how to, "Create list of single item repeated n times." - in general, sometimes an answer may be applicable to more than one question. – Russia Must Remove Putin Aug 28 '23 at 21:13
  • Ah, I see, you're just trying to motivate why the aliasing might sometimes be deliberate/beneficial. – Karl Knechtel Aug 29 '23 at 00:21
30

Itertools has a function just for that:

import itertools
it = itertools.repeat(e,n)

Of course itertools gives you a iterator instead of a list. [e] * n gives you a list, but, depending on what you will do with those sequences, the itertools variant can be much more efficient.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Jochen Ritzel
  • 104,512
  • 31
  • 200
  • 194
18

As others have pointed out, using the * operator for a mutable object duplicates references, so if you change one you change them all. If you want to create independent instances of a mutable object, your xrange syntax is the most Pythonic way to do this. If you are bothered by having a named variable that is never used, you can use the anonymous underscore variable.

[e for _ in xrange(n)]
W.P. McNeill
  • 16,336
  • 12
  • 75
  • 111
  • Should be ```range()``` instead ```xrange()```. Example: my_list``` = [1 for _ in range(10)]; and ```my_list``` will be [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] – cbontoiu May 13 '22 at 16:43
13
[e] * n

should work

Mad Scientist
  • 18,090
  • 12
  • 83
  • 109
9

If you are looking for a simple repeat like:

[1, 2, 3, 1, 2, 3, 1, 2, 3]

simply use:

[1, 2, 3] * 3

But if you are seeking for:

[1, 1, 1, 2, 2, 2, 3, 3, 3]

This one is better while takes more time:

numpy.concatenate([([i]*3) for i in [1,2,3]], axis=0)
Shrm
  • 426
  • 4
  • 8
3

Sorry for my really late answer You can use numpy.repeat easily. Just by writing the value that you would like to produce and the number of repetition.

import numpy as np
x = [1,2,3]
y = np.linspace(0,1000,10000)
for i in x:
    new_x = np.repeat(i,len(y))
    print(new_x)
john22
  • 375
  • 2
  • 13
  • If you are already using Numpy for other reasons, this one is good to know (but trivial enough to be obvious to those who need it). For solving the problem from scratch, adding a huge third-party library just for this is silly at best and irresponsible at worst. – tripleee Aug 26 '23 at 18:10
3

If you're seeking

[1, 1, 1, 2, 2, 2, 3, 3, 3]

without numpy, you can use the builtin itertools module

from itertools import chain
list(chain.from_iterable(zip(*[[1,2,3]]*3)))

With a simple list comprehension (without even itertools)

[e for x in zip(*[[1,2,3]]*3) for e in x]
cottontail
  • 10,268
  • 18
  • 50
  • 51
-1
import numpy as np
a = np.full(fill_value=1, shape=3)
print(a)
b = np.repeat(a=[1,2,3],repeats=3)
print(b)
c = np.repeat(a=[[1,2,3]],repeats=3,axis=0).flatten()
print(c)
Say OL
  • 234
  • 1
  • 3
  • 10
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jun 11 '23 at 01:18
  • That's even more important here, given how many answers have already been received. How does your approach differ from the other answers given? Why do you prefer this approach? Is it better suited for particular situations than other answers? – Jeremy Caney Jun 11 '23 at 01:19