2

I have a list, and I want to repeat its elements in the same order.

I want to create bg using sl.

# small list
sl = [10,20]
# from small list, create a big list
bg = [10,10,20,20]

I tried:

bg = [[i,j] in for i,j in zip([[sl[0]]*2,[sl[1]]*2])]

But this gives me:

 bg = [[10,20],[10,20]]

How can I get the desired output?

BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
Mainland
  • 4,110
  • 3
  • 25
  • 56

5 Answers5

4
import numpy as np
s1 = [10,20]
s2 = np.array(s1).repeat(2)
print(list(s2)) # [10, 10, 20, 20]

I could not resist the urge to use numpy in such operations. With this function you can repeat not only for single dimensional cases but also in case of matrix or higher order tensors as well.

user2736738
  • 30,591
  • 5
  • 42
  • 56
1

Here is an approach using itertools:

from itertools import repeat, chain
repetitions = 2

list(chain.from_iterable(repeat(item, repetitions) for item in sl))

You can also use a list comprehension, though it's important to note that using multiple for clauses in a list comprehension is considered poor style by some:

[item for item in sl for _ in range(repetitions)]

This outputs:

[10, 10, 20, 20]
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
1

You can use itertools.chain and zip:

from itertools import chain
out = list(chain.from_iterable(zip(*[sl]*2)))

Output:

[10, 10, 20, 20]

Since you tagged it pandas, you could use repeat as well:

out = pd.Series(sl).repeat(2)

Output:

0    10
0    10
1    20
1    20
dtype: int64
  • pandas seem to be the simple approach. The other one needs new modules import, looks a bit complex. The only thing is I need to convert this `.tolist()` – Mainland May 06 '22 at 04:34
  • okay. Which one do you consider the python way of doing it and takes less time and resources? – Mainland May 06 '22 at 04:40
  • use `np.repeat` if you are keen on performance, unless of course you really need lists, and have no further processing to do – sammywemmy May 06 '22 at 04:41
  • @sammywemmy new solution using numpy? interesting. How do we do that? – Mainland May 06 '22 at 04:43
  • Can you explain a bit about what is data type conversion and other types implied here? – Mainland May 06 '22 at 04:50
  • @Mainland I meant Python list, numpy array, pandas Series etc. If you plan to do a simple operation on an object (filtering, indexing etc), the efficiency very much depends on what data type it is. Check out [this post](https://stackoverflow.com/a/70965024/17521785). If the initial data is a list, then even though numpy is extremely fast, converting the list to numpy array outweighs whatever gains that may be gotten from converting to an array. –  May 06 '22 at 05:02
  • 1
    Wrap the list duplication in a list, this should be faster: `list(chain.from_iterable(zip(*[[sl]*1000000])))` – mozway May 06 '22 at 05:07
1

A very simple approach might be:

sl = [10,20]
# from small list, create a big list
bg = []
for ele in sl:
    bg.append(ele)
    bg.append(ele)
print(bg)

To make it more generic and incase of more repetitions, you can do:

def append_lst(lst, val):
    lst.append(val)

sl = [10,20]
# from small list, create a big list
bg = []
noOfRep = 2
for ele in sl:
    for _ in range(noOfRep):
        append_lst(bg, ele)
    
print(bg)

Output:

[10, 10, 20, 20]
Avinash
  • 875
  • 6
  • 20
0
l1 = [10,20]
l2 = l1
l1.extend(l2)

output:

value of l1:
[10,20,10,20]
Raj Patel
  • 154
  • 6