The ideal way is probably numpy.repeat:
In [16]:
import numpy as np
x1=[1,2,3,4]
In [17]:
np.repeat(x1,3)
Out[17]:
array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])
Answer from CT Zhu on Stack Overflowpython - Repeating elements of a list n times - Stack Overflow
python - Create list of single item repeated N times - Stack Overflow
function to find repeating numbers in list
Repeat function
Videos
The ideal way is probably numpy.repeat:
In [16]:
import numpy as np
x1=[1,2,3,4]
In [17]:
np.repeat(x1,3)
Out[17]:
array([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])
In case you really want result as list, and generator is not sufficient:
import itertools
lst = range(1,5)
list(itertools.chain.from_iterable(itertools.repeat(x, 3) for x in lst))
Out[8]: [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
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.
>>> [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]]
Hello to everyone! We had programming test today and one of the tasks was to write function which can find duplicate numbers in lists. I wrote something like this and it was OK:
def seznam_duplicit(spisok: int): seznam_duplicit = [] for cislo1 in spisok: spisok.remove(cislo1) for cislo2 in spisok: if cislo1 == cislo2: if cislo1 not in seznam_duplicit: seznam_duplicit.append(cislo1) return(seznam_duplicit)
But now I’m trying it on my VS code and when I put up list [1, 2, 4, 5, 3, 2, 4, 1] it returns list [1, 4]. So it lost 2. I’ve tried other lists and it always looses one duplicate number. What can I do to fix this?
Hi, I am very new to python and my current problem is that I want to be able to repeat a certain line of code and not the entire program until something else becomes true, I've done some digging online and cant find a solution that I understand properly, the code pasted has a comment on line 22, that is where I want a repeat function to go, the comment goes more in-depth. Here is a link to the code
https://pastebin.com/M46X2awm