nums = [1,2,3,4,5,6]
replace = 1
for x in nums:
x = replace
print(x)
How can I replace all nums to 1:
nums = [1,1,1,1,1,1]
If the number (in this case, 1) is known, just re-assign it like this:
replace = 1
nums = [replace]*len(nums)
This is way much faster than iteration as suggested in other answers in case of too many numbers.
>>> start=time.time(); a = [1 for _ in range(1000000)]; print(time.time() - start)
0.039171695709228516
>>> start=time.time(); a = [1] * 1000000; print(time.time() - start)
0.0036449432373046875
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With