4

So I have a list [a,b,c] and I want to obtain [a,b,c,a,b,c,...a,b,c].

I can of course do this with two nested loops, but there must be a better way? itertools.cycle() would have been solution if I could provide a count.

Two constraints:

  1. it should work in 2.7 (but for the sake of curiosity I'm interested in a 3.x solution)
  2. list elements should be independent copies (they are mutable types)
Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
xenoid
  • 8,396
  • 3
  • 23
  • 49
  • @`itertools.cycle()` won't make copies of the elements. "Better" is meaningless without any comparison criteria. – Stop harming Monica Oct 04 '17 at 08:47
  • Related questions: [Circular list iterator in Python](https://stackoverflow.com/q/23416381/7851470), [Create list of single item repeated N times](https://stackoverflow.com/q/3459098/7851470), [Repeat a list within a list X number of times](https://stackoverflow.com/q/16095865/7851470), [Repeating elements of a list n times](https://stackoverflow.com/q/24225072/7851470). – Georgy Oct 01 '19 at 21:13

3 Answers3

7

Eventually I came up with:

[copy.copy(e) for _ in range(N) for e in sourceList]
xenoid
  • 8,396
  • 3
  • 23
  • 49
4

For immutable types, you can use multiplication operator on the list:

>>> [1,2,3]*5
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

for mutable types, you need to perform a copy of the elements prior to inserting.

I would chain a repeated copy of the items of the list.

import copy,itertools

a=[12]
b=[13]
c=[14]

l = [a,b,c]

new_l = list(itertools.chain.from_iterable(map(copy.copy,l) for _ in range(5)))

in new_l, all lists are independent (references are not copies from each other). You may need copy.deepcopy in some cases.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
-1

To start with, if one has a list = [a, b, c], if one prints it, one will get the following error

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-17-eb02c90df247> in <module>
----> 1 list = [a, b, c]
      2 list

NameError: name 'c' is not defined

The list should be defined as follows

>>> list = ['a', 'b', 'c']
>>> print(list)
['a', 'b', 'c']

For your case, simply multiplying the list with N will do the work. For the examples, let's consider N=3

>>> n = 3
>>> list = list*n
['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']

Now, and giving an alternative answer, as the need may arise, if one wants to extend the list (considering, for the example, N=3) as follows

['a' 'a' 'a' 'b' 'b' 'b' 'c' 'c' 'c']

One can use np.repeat do that

>>> n = 3
>>> list= np.repeat(list, n)
array(['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c'], dtype='<U1')

This retrieved an array. If that doesn't fit one's needs and one wants a list (use np.ndarray.tolist), as follows

>>> list = list.tolist()
['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']
Gonçalo Peres
  • 11,752
  • 3
  • 54
  • 83
  • 1
    1) consider that a,b,and c are variables referencing mutable objects. 2) the resulting list hasn't got independent elements, if I modify one this also modifies the "copies". – xenoid Jan 21 '21 at 11:43