Periodically replacing values in a list
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
Suppose I have the following list in Python:
my_list = [10] * 95
Given n
, I want to replace any other m
elements with zero in my list, while keeping the next n
elements.
For example, if n = 3
and m = 2
, I want my list to look like:
[10, 10, 10, 0, 0, 10, 10, 10 ,0, 0, ..., 10, 10, 10 , 0, 0]
If it can't be filled perfectly, as is the case with n = 4
and m = 2
, then it's OK if my list looks like this:
[10, 10, 10, 10, 0, 0, ..., 10, 10, 10, 10, 0]
How should I try to solve this problem?
python list replace
add a comment |Â
up vote
7
down vote
favorite
Suppose I have the following list in Python:
my_list = [10] * 95
Given n
, I want to replace any other m
elements with zero in my list, while keeping the next n
elements.
For example, if n = 3
and m = 2
, I want my list to look like:
[10, 10, 10, 0, 0, 10, 10, 10 ,0, 0, ..., 10, 10, 10 , 0, 0]
If it can't be filled perfectly, as is the case with n = 4
and m = 2
, then it's OK if my list looks like this:
[10, 10, 10, 10, 0, 0, ..., 10, 10, 10, 10, 0]
How should I try to solve this problem?
python list replace
1
How about you write a program?
– Julien
1 hour ago
Will the input list always be filled with one and the same value (like 10 in your example)? In other words, do you want to create a new list like[10, 10, 10, 0, 0, ...]
or do you want to overwrite every nth value in an existing list?
– Aran-Fey
58 mins ago
@Aran-Fey 10 is indeed just an example. But the input list will stay constant. So it can be any number, but it will always be the same one. It doesn't really matter to me if the list gets overwritten or if a new one is created.
– Riley
57 mins ago
Is the length of the list going to be the same?
– alec_djinn
53 mins ago
@alec_djinn, yes it will. It would be nice to see a more general method, but if it works for a specific length, that's also ok.
– Riley
48 mins ago
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
Suppose I have the following list in Python:
my_list = [10] * 95
Given n
, I want to replace any other m
elements with zero in my list, while keeping the next n
elements.
For example, if n = 3
and m = 2
, I want my list to look like:
[10, 10, 10, 0, 0, 10, 10, 10 ,0, 0, ..., 10, 10, 10 , 0, 0]
If it can't be filled perfectly, as is the case with n = 4
and m = 2
, then it's OK if my list looks like this:
[10, 10, 10, 10, 0, 0, ..., 10, 10, 10, 10, 0]
How should I try to solve this problem?
python list replace
Suppose I have the following list in Python:
my_list = [10] * 95
Given n
, I want to replace any other m
elements with zero in my list, while keeping the next n
elements.
For example, if n = 3
and m = 2
, I want my list to look like:
[10, 10, 10, 0, 0, 10, 10, 10 ,0, 0, ..., 10, 10, 10 , 0, 0]
If it can't be filled perfectly, as is the case with n = 4
and m = 2
, then it's OK if my list looks like this:
[10, 10, 10, 10, 0, 0, ..., 10, 10, 10, 10, 0]
How should I try to solve this problem?
python list replace
python list replace
edited 1 hour ago


Aran-Fey
20.4k53063
20.4k53063
asked 1 hour ago
Riley
19811
19811
1
How about you write a program?
– Julien
1 hour ago
Will the input list always be filled with one and the same value (like 10 in your example)? In other words, do you want to create a new list like[10, 10, 10, 0, 0, ...]
or do you want to overwrite every nth value in an existing list?
– Aran-Fey
58 mins ago
@Aran-Fey 10 is indeed just an example. But the input list will stay constant. So it can be any number, but it will always be the same one. It doesn't really matter to me if the list gets overwritten or if a new one is created.
– Riley
57 mins ago
Is the length of the list going to be the same?
– alec_djinn
53 mins ago
@alec_djinn, yes it will. It would be nice to see a more general method, but if it works for a specific length, that's also ok.
– Riley
48 mins ago
add a comment |Â
1
How about you write a program?
– Julien
1 hour ago
Will the input list always be filled with one and the same value (like 10 in your example)? In other words, do you want to create a new list like[10, 10, 10, 0, 0, ...]
or do you want to overwrite every nth value in an existing list?
– Aran-Fey
58 mins ago
@Aran-Fey 10 is indeed just an example. But the input list will stay constant. So it can be any number, but it will always be the same one. It doesn't really matter to me if the list gets overwritten or if a new one is created.
– Riley
57 mins ago
Is the length of the list going to be the same?
– alec_djinn
53 mins ago
@alec_djinn, yes it will. It would be nice to see a more general method, but if it works for a specific length, that's also ok.
– Riley
48 mins ago
1
1
How about you write a program?
– Julien
1 hour ago
How about you write a program?
– Julien
1 hour ago
Will the input list always be filled with one and the same value (like 10 in your example)? In other words, do you want to create a new list like
[10, 10, 10, 0, 0, ...]
or do you want to overwrite every nth value in an existing list?– Aran-Fey
58 mins ago
Will the input list always be filled with one and the same value (like 10 in your example)? In other words, do you want to create a new list like
[10, 10, 10, 0, 0, ...]
or do you want to overwrite every nth value in an existing list?– Aran-Fey
58 mins ago
@Aran-Fey 10 is indeed just an example. But the input list will stay constant. So it can be any number, but it will always be the same one. It doesn't really matter to me if the list gets overwritten or if a new one is created.
– Riley
57 mins ago
@Aran-Fey 10 is indeed just an example. But the input list will stay constant. So it can be any number, but it will always be the same one. It doesn't really matter to me if the list gets overwritten or if a new one is created.
– Riley
57 mins ago
Is the length of the list going to be the same?
– alec_djinn
53 mins ago
Is the length of the list going to be the same?
– alec_djinn
53 mins ago
@alec_djinn, yes it will. It would be nice to see a more general method, but if it works for a specific length, that's also ok.
– Riley
48 mins ago
@alec_djinn, yes it will. It would be nice to see a more general method, but if it works for a specific length, that's also ok.
– Riley
48 mins ago
add a comment |Â
7 Answers
7
active
oldest
votes
up vote
5
down vote
accepted
my_list = [10] * 95
n = 3
m = 2
for i in range(m):
my_list[n+i::m+n] = [0] * len(my_list[n+i::m+n])
If you really just have two possible values (e. g. 10 and 0), you can do it even simpler:
my_list = [ 10 if i % (n+m) < n else 0 for i in range(95) ]
A bit more complex but probably more efficient (especially for huge lists and large values for n
and m
) would be this:
my_list = (([ 10 ] * n + [ 0 ] * m) * (95 // (n + m) + 1))[:95]
Could you post your 3 different solutions as 3 separate answers, please? I want to upvote the 2nd one, but the others... not so much.
– Aran-Fey
27 mins ago
add a comment |Â
up vote
3
down vote
This worked for me:
list = [10] * 95
n = 4
m = 2
amask = np.tile(np.concatenate((np.ones(n),np.zeros(m))),int((len(list)+1)/(n+m)))[:len(list)]
list = np.asarray(list)*amask
which outputs:
array([10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10.,
10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10.,
10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10.,
10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10.,
0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0.,
0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0.,
10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10.,
10., 10., 10., 0.])
The code takes n
and m
and constructs a mask of ones and zeros with a length matching your initial list
using the np.tile
function. Afterwards you just multiply the mask onto the list and get the zeros where you want them to be. It should also be flexibel to different lengths of the list and an (almost) arbitrary choice of n
and m
.
You can cast the array back to a list if you want.
1
numpy
? For real?
– Alfe
52 mins ago
add a comment |Â
up vote
3
down vote
You could use itertools.cycle
to create an endless sequence of [10, 10, 10, 0, 0]
and then take the first 95 elements of that sequence with itertools.islice
:
n = 3
m = 2
pattern = [10] * n + [0] * m
my_list = list(itertools.islice(itertools.cycle(pattern), 95))
Why use itertools when you can use list expression?
– Moonsik Park
42 mins ago
@MoonsikPark Not sure what you mean. If you're referring to your own solution: Because I think mine is more readable than yours.
– Aran-Fey
41 mins ago
Creative! Itertools is a nice touch. +1
– Riley
32 mins ago
add a comment |Â
up vote
2
down vote
How about this?
my_list = [10] * 95
n = 3
m = 2
for i in range(n, len(my_list)-1, n+m):
my_list[i:i+m] = [0]*m
print(my_list)
Edit
I found out that the above code changes the length of resulting list in some cases.
>>> a = [1,2,3]
>>> a[2:4] = [0] * 2
>>> a
[1, 2, 0, 0]
Thus, the length should be restored somehow.
my_list = [10] * 95
cp_list = list(my_list)
n = 3
m = 5
for i in range(n, len(my_list)-1, n+m):
cp_list[i:i+m] = [0]*m
cp_list = cp_list[:len(my_list)]
print(cp_list)
Nice approach. But only iflen(my_list)
is smaller thanm
, this is more efficient than my solution.
– Alfe
46 mins ago
Nice! +1 For clarity.
– Riley
45 mins ago
There is a downvote on this answer but no critic comment. Downvoter, please comment on what you didn't like.
– Alfe
41 mins ago
add a comment |Â
up vote
2
down vote
numpy
can do this pretty concisely, too!
a = np.array(my_list).reshape(-1, n + m)
a[:, n:] = 0
result = a.ravel().tolist()
add a comment |Â
up vote
1
down vote
So many solutions! Here is mine.
l = [10]*95
n = 4
m = 2
for i,x in enumerate(l):
if not i%(n+m) and i != 0:
l[i-m:i] = [0]*m
print(l)
[10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 10]
if not i%(n+m)
is used to check for indexes that are multiple of n+m
. Once a good index is found, replace the previous m
entries to the value you want. and i != 0
simply skip the first index since 0%X
is always 0.
This is just a less efficient and less readable version of therange
answer.
– Aran-Fey
29 mins ago
I didn't look for efficiency indeed, but about readability, I disagree, I personally find this answer way more readable and easy to understand. It's a personal opinion of course.
– alec_djinn
26 mins ago
The output should be[..., 0, 0, 10, 10, 10, 10, 0]
, not[..., 0, 0, 10, 10, 10, 10, 10]
.
– klim
20 mins ago
add a comment |Â
up vote
0
down vote
[j for i in [[input_num] * n + [0] * m for x in range(int(num / (m + n)) + 1)][:num] for j in i]
Maybe?
Result
>>> num, input_num, m, n=95, 10, 2, 3
>>> [j for i in [[input_num] * n + [0] * m for x in range(int(num / (m + n)) + 1)][:num] for j in i]
[10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0 , 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0]
2
I guess the list full of10
is just for an example...
– Julien
1 hour ago
@Julien No, OP said ' it can be any number, but it will always be the same one' so it will be full of any same number.
– Moonsik Park
50 mins ago
1
1) Your output is incorrect (it's[10, 10, 0, 0, 0]
instead of[10, 10, 10, 0, 0]
) 2) Concatenating lists withsum
is an antipattern with a massive run time complexity.
– Aran-Fey
36 mins ago
@Aran-Fey Nope, it's correct.
– Moonsik Park
31 mins ago
Oops. Indeed it is. Edited.
– Moonsik Park
26 mins ago
add a comment |Â
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
my_list = [10] * 95
n = 3
m = 2
for i in range(m):
my_list[n+i::m+n] = [0] * len(my_list[n+i::m+n])
If you really just have two possible values (e. g. 10 and 0), you can do it even simpler:
my_list = [ 10 if i % (n+m) < n else 0 for i in range(95) ]
A bit more complex but probably more efficient (especially for huge lists and large values for n
and m
) would be this:
my_list = (([ 10 ] * n + [ 0 ] * m) * (95 // (n + m) + 1))[:95]
Could you post your 3 different solutions as 3 separate answers, please? I want to upvote the 2nd one, but the others... not so much.
– Aran-Fey
27 mins ago
add a comment |Â
up vote
5
down vote
accepted
my_list = [10] * 95
n = 3
m = 2
for i in range(m):
my_list[n+i::m+n] = [0] * len(my_list[n+i::m+n])
If you really just have two possible values (e. g. 10 and 0), you can do it even simpler:
my_list = [ 10 if i % (n+m) < n else 0 for i in range(95) ]
A bit more complex but probably more efficient (especially for huge lists and large values for n
and m
) would be this:
my_list = (([ 10 ] * n + [ 0 ] * m) * (95 // (n + m) + 1))[:95]
Could you post your 3 different solutions as 3 separate answers, please? I want to upvote the 2nd one, but the others... not so much.
– Aran-Fey
27 mins ago
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
my_list = [10] * 95
n = 3
m = 2
for i in range(m):
my_list[n+i::m+n] = [0] * len(my_list[n+i::m+n])
If you really just have two possible values (e. g. 10 and 0), you can do it even simpler:
my_list = [ 10 if i % (n+m) < n else 0 for i in range(95) ]
A bit more complex but probably more efficient (especially for huge lists and large values for n
and m
) would be this:
my_list = (([ 10 ] * n + [ 0 ] * m) * (95 // (n + m) + 1))[:95]
my_list = [10] * 95
n = 3
m = 2
for i in range(m):
my_list[n+i::m+n] = [0] * len(my_list[n+i::m+n])
If you really just have two possible values (e. g. 10 and 0), you can do it even simpler:
my_list = [ 10 if i % (n+m) < n else 0 for i in range(95) ]
A bit more complex but probably more efficient (especially for huge lists and large values for n
and m
) would be this:
my_list = (([ 10 ] * n + [ 0 ] * m) * (95 // (n + m) + 1))[:95]
edited 36 mins ago
answered 48 mins ago


Alfe
29.5k105997
29.5k105997
Could you post your 3 different solutions as 3 separate answers, please? I want to upvote the 2nd one, but the others... not so much.
– Aran-Fey
27 mins ago
add a comment |Â
Could you post your 3 different solutions as 3 separate answers, please? I want to upvote the 2nd one, but the others... not so much.
– Aran-Fey
27 mins ago
Could you post your 3 different solutions as 3 separate answers, please? I want to upvote the 2nd one, but the others... not so much.
– Aran-Fey
27 mins ago
Could you post your 3 different solutions as 3 separate answers, please? I want to upvote the 2nd one, but the others... not so much.
– Aran-Fey
27 mins ago
add a comment |Â
up vote
3
down vote
This worked for me:
list = [10] * 95
n = 4
m = 2
amask = np.tile(np.concatenate((np.ones(n),np.zeros(m))),int((len(list)+1)/(n+m)))[:len(list)]
list = np.asarray(list)*amask
which outputs:
array([10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10.,
10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10.,
10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10.,
10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10.,
0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0.,
0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0.,
10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10.,
10., 10., 10., 0.])
The code takes n
and m
and constructs a mask of ones and zeros with a length matching your initial list
using the np.tile
function. Afterwards you just multiply the mask onto the list and get the zeros where you want them to be. It should also be flexibel to different lengths of the list and an (almost) arbitrary choice of n
and m
.
You can cast the array back to a list if you want.
1
numpy
? For real?
– Alfe
52 mins ago
add a comment |Â
up vote
3
down vote
This worked for me:
list = [10] * 95
n = 4
m = 2
amask = np.tile(np.concatenate((np.ones(n),np.zeros(m))),int((len(list)+1)/(n+m)))[:len(list)]
list = np.asarray(list)*amask
which outputs:
array([10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10.,
10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10.,
10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10.,
10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10.,
0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0.,
0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0.,
10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10.,
10., 10., 10., 0.])
The code takes n
and m
and constructs a mask of ones and zeros with a length matching your initial list
using the np.tile
function. Afterwards you just multiply the mask onto the list and get the zeros where you want them to be. It should also be flexibel to different lengths of the list and an (almost) arbitrary choice of n
and m
.
You can cast the array back to a list if you want.
1
numpy
? For real?
– Alfe
52 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
This worked for me:
list = [10] * 95
n = 4
m = 2
amask = np.tile(np.concatenate((np.ones(n),np.zeros(m))),int((len(list)+1)/(n+m)))[:len(list)]
list = np.asarray(list)*amask
which outputs:
array([10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10.,
10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10.,
10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10.,
10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10.,
0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0.,
0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0.,
10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10.,
10., 10., 10., 0.])
The code takes n
and m
and constructs a mask of ones and zeros with a length matching your initial list
using the np.tile
function. Afterwards you just multiply the mask onto the list and get the zeros where you want them to be. It should also be flexibel to different lengths of the list and an (almost) arbitrary choice of n
and m
.
You can cast the array back to a list if you want.
This worked for me:
list = [10] * 95
n = 4
m = 2
amask = np.tile(np.concatenate((np.ones(n),np.zeros(m))),int((len(list)+1)/(n+m)))[:len(list)]
list = np.asarray(list)*amask
which outputs:
array([10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10.,
10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10.,
10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10.,
10., 0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10.,
0., 0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0.,
0., 10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0.,
10., 10., 10., 10., 0., 0., 10., 10., 10., 10., 0., 0., 10.,
10., 10., 10., 0.])
The code takes n
and m
and constructs a mask of ones and zeros with a length matching your initial list
using the np.tile
function. Afterwards you just multiply the mask onto the list and get the zeros where you want them to be. It should also be flexibel to different lengths of the list and an (almost) arbitrary choice of n
and m
.
You can cast the array back to a list if you want.
edited 49 mins ago
answered 53 mins ago
Shintlor
1949
1949
1
numpy
? For real?
– Alfe
52 mins ago
add a comment |Â
1
numpy
? For real?
– Alfe
52 mins ago
1
1
numpy
? For real?– Alfe
52 mins ago
numpy
? For real?– Alfe
52 mins ago
add a comment |Â
up vote
3
down vote
You could use itertools.cycle
to create an endless sequence of [10, 10, 10, 0, 0]
and then take the first 95 elements of that sequence with itertools.islice
:
n = 3
m = 2
pattern = [10] * n + [0] * m
my_list = list(itertools.islice(itertools.cycle(pattern), 95))
Why use itertools when you can use list expression?
– Moonsik Park
42 mins ago
@MoonsikPark Not sure what you mean. If you're referring to your own solution: Because I think mine is more readable than yours.
– Aran-Fey
41 mins ago
Creative! Itertools is a nice touch. +1
– Riley
32 mins ago
add a comment |Â
up vote
3
down vote
You could use itertools.cycle
to create an endless sequence of [10, 10, 10, 0, 0]
and then take the first 95 elements of that sequence with itertools.islice
:
n = 3
m = 2
pattern = [10] * n + [0] * m
my_list = list(itertools.islice(itertools.cycle(pattern), 95))
Why use itertools when you can use list expression?
– Moonsik Park
42 mins ago
@MoonsikPark Not sure what you mean. If you're referring to your own solution: Because I think mine is more readable than yours.
– Aran-Fey
41 mins ago
Creative! Itertools is a nice touch. +1
– Riley
32 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You could use itertools.cycle
to create an endless sequence of [10, 10, 10, 0, 0]
and then take the first 95 elements of that sequence with itertools.islice
:
n = 3
m = 2
pattern = [10] * n + [0] * m
my_list = list(itertools.islice(itertools.cycle(pattern), 95))
You could use itertools.cycle
to create an endless sequence of [10, 10, 10, 0, 0]
and then take the first 95 elements of that sequence with itertools.islice
:
n = 3
m = 2
pattern = [10] * n + [0] * m
my_list = list(itertools.islice(itertools.cycle(pattern), 95))
answered 43 mins ago


Aran-Fey
20.4k53063
20.4k53063
Why use itertools when you can use list expression?
– Moonsik Park
42 mins ago
@MoonsikPark Not sure what you mean. If you're referring to your own solution: Because I think mine is more readable than yours.
– Aran-Fey
41 mins ago
Creative! Itertools is a nice touch. +1
– Riley
32 mins ago
add a comment |Â
Why use itertools when you can use list expression?
– Moonsik Park
42 mins ago
@MoonsikPark Not sure what you mean. If you're referring to your own solution: Because I think mine is more readable than yours.
– Aran-Fey
41 mins ago
Creative! Itertools is a nice touch. +1
– Riley
32 mins ago
Why use itertools when you can use list expression?
– Moonsik Park
42 mins ago
Why use itertools when you can use list expression?
– Moonsik Park
42 mins ago
@MoonsikPark Not sure what you mean. If you're referring to your own solution: Because I think mine is more readable than yours.
– Aran-Fey
41 mins ago
@MoonsikPark Not sure what you mean. If you're referring to your own solution: Because I think mine is more readable than yours.
– Aran-Fey
41 mins ago
Creative! Itertools is a nice touch. +1
– Riley
32 mins ago
Creative! Itertools is a nice touch. +1
– Riley
32 mins ago
add a comment |Â
up vote
2
down vote
How about this?
my_list = [10] * 95
n = 3
m = 2
for i in range(n, len(my_list)-1, n+m):
my_list[i:i+m] = [0]*m
print(my_list)
Edit
I found out that the above code changes the length of resulting list in some cases.
>>> a = [1,2,3]
>>> a[2:4] = [0] * 2
>>> a
[1, 2, 0, 0]
Thus, the length should be restored somehow.
my_list = [10] * 95
cp_list = list(my_list)
n = 3
m = 5
for i in range(n, len(my_list)-1, n+m):
cp_list[i:i+m] = [0]*m
cp_list = cp_list[:len(my_list)]
print(cp_list)
Nice approach. But only iflen(my_list)
is smaller thanm
, this is more efficient than my solution.
– Alfe
46 mins ago
Nice! +1 For clarity.
– Riley
45 mins ago
There is a downvote on this answer but no critic comment. Downvoter, please comment on what you didn't like.
– Alfe
41 mins ago
add a comment |Â
up vote
2
down vote
How about this?
my_list = [10] * 95
n = 3
m = 2
for i in range(n, len(my_list)-1, n+m):
my_list[i:i+m] = [0]*m
print(my_list)
Edit
I found out that the above code changes the length of resulting list in some cases.
>>> a = [1,2,3]
>>> a[2:4] = [0] * 2
>>> a
[1, 2, 0, 0]
Thus, the length should be restored somehow.
my_list = [10] * 95
cp_list = list(my_list)
n = 3
m = 5
for i in range(n, len(my_list)-1, n+m):
cp_list[i:i+m] = [0]*m
cp_list = cp_list[:len(my_list)]
print(cp_list)
Nice approach. But only iflen(my_list)
is smaller thanm
, this is more efficient than my solution.
– Alfe
46 mins ago
Nice! +1 For clarity.
– Riley
45 mins ago
There is a downvote on this answer but no critic comment. Downvoter, please comment on what you didn't like.
– Alfe
41 mins ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
How about this?
my_list = [10] * 95
n = 3
m = 2
for i in range(n, len(my_list)-1, n+m):
my_list[i:i+m] = [0]*m
print(my_list)
Edit
I found out that the above code changes the length of resulting list in some cases.
>>> a = [1,2,3]
>>> a[2:4] = [0] * 2
>>> a
[1, 2, 0, 0]
Thus, the length should be restored somehow.
my_list = [10] * 95
cp_list = list(my_list)
n = 3
m = 5
for i in range(n, len(my_list)-1, n+m):
cp_list[i:i+m] = [0]*m
cp_list = cp_list[:len(my_list)]
print(cp_list)
How about this?
my_list = [10] * 95
n = 3
m = 2
for i in range(n, len(my_list)-1, n+m):
my_list[i:i+m] = [0]*m
print(my_list)
Edit
I found out that the above code changes the length of resulting list in some cases.
>>> a = [1,2,3]
>>> a[2:4] = [0] * 2
>>> a
[1, 2, 0, 0]
Thus, the length should be restored somehow.
my_list = [10] * 95
cp_list = list(my_list)
n = 3
m = 5
for i in range(n, len(my_list)-1, n+m):
cp_list[i:i+m] = [0]*m
cp_list = cp_list[:len(my_list)]
print(cp_list)
edited 37 mins ago
answered 52 mins ago
klim
46339
46339
Nice approach. But only iflen(my_list)
is smaller thanm
, this is more efficient than my solution.
– Alfe
46 mins ago
Nice! +1 For clarity.
– Riley
45 mins ago
There is a downvote on this answer but no critic comment. Downvoter, please comment on what you didn't like.
– Alfe
41 mins ago
add a comment |Â
Nice approach. But only iflen(my_list)
is smaller thanm
, this is more efficient than my solution.
– Alfe
46 mins ago
Nice! +1 For clarity.
– Riley
45 mins ago
There is a downvote on this answer but no critic comment. Downvoter, please comment on what you didn't like.
– Alfe
41 mins ago
Nice approach. But only if
len(my_list)
is smaller than m
, this is more efficient than my solution.– Alfe
46 mins ago
Nice approach. But only if
len(my_list)
is smaller than m
, this is more efficient than my solution.– Alfe
46 mins ago
Nice! +1 For clarity.
– Riley
45 mins ago
Nice! +1 For clarity.
– Riley
45 mins ago
There is a downvote on this answer but no critic comment. Downvoter, please comment on what you didn't like.
– Alfe
41 mins ago
There is a downvote on this answer but no critic comment. Downvoter, please comment on what you didn't like.
– Alfe
41 mins ago
add a comment |Â
up vote
2
down vote
numpy
can do this pretty concisely, too!
a = np.array(my_list).reshape(-1, n + m)
a[:, n:] = 0
result = a.ravel().tolist()
add a comment |Â
up vote
2
down vote
numpy
can do this pretty concisely, too!
a = np.array(my_list).reshape(-1, n + m)
a[:, n:] = 0
result = a.ravel().tolist()
add a comment |Â
up vote
2
down vote
up vote
2
down vote
numpy
can do this pretty concisely, too!
a = np.array(my_list).reshape(-1, n + m)
a[:, n:] = 0
result = a.ravel().tolist()
numpy
can do this pretty concisely, too!
a = np.array(my_list).reshape(-1, n + m)
a[:, n:] = 0
result = a.ravel().tolist()
answered 28 mins ago


timgeb
37.1k104875
37.1k104875
add a comment |Â
add a comment |Â
up vote
1
down vote
So many solutions! Here is mine.
l = [10]*95
n = 4
m = 2
for i,x in enumerate(l):
if not i%(n+m) and i != 0:
l[i-m:i] = [0]*m
print(l)
[10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 10]
if not i%(n+m)
is used to check for indexes that are multiple of n+m
. Once a good index is found, replace the previous m
entries to the value you want. and i != 0
simply skip the first index since 0%X
is always 0.
This is just a less efficient and less readable version of therange
answer.
– Aran-Fey
29 mins ago
I didn't look for efficiency indeed, but about readability, I disagree, I personally find this answer way more readable and easy to understand. It's a personal opinion of course.
– alec_djinn
26 mins ago
The output should be[..., 0, 0, 10, 10, 10, 10, 0]
, not[..., 0, 0, 10, 10, 10, 10, 10]
.
– klim
20 mins ago
add a comment |Â
up vote
1
down vote
So many solutions! Here is mine.
l = [10]*95
n = 4
m = 2
for i,x in enumerate(l):
if not i%(n+m) and i != 0:
l[i-m:i] = [0]*m
print(l)
[10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 10]
if not i%(n+m)
is used to check for indexes that are multiple of n+m
. Once a good index is found, replace the previous m
entries to the value you want. and i != 0
simply skip the first index since 0%X
is always 0.
This is just a less efficient and less readable version of therange
answer.
– Aran-Fey
29 mins ago
I didn't look for efficiency indeed, but about readability, I disagree, I personally find this answer way more readable and easy to understand. It's a personal opinion of course.
– alec_djinn
26 mins ago
The output should be[..., 0, 0, 10, 10, 10, 10, 0]
, not[..., 0, 0, 10, 10, 10, 10, 10]
.
– klim
20 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
So many solutions! Here is mine.
l = [10]*95
n = 4
m = 2
for i,x in enumerate(l):
if not i%(n+m) and i != 0:
l[i-m:i] = [0]*m
print(l)
[10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 10]
if not i%(n+m)
is used to check for indexes that are multiple of n+m
. Once a good index is found, replace the previous m
entries to the value you want. and i != 0
simply skip the first index since 0%X
is always 0.
So many solutions! Here is mine.
l = [10]*95
n = 4
m = 2
for i,x in enumerate(l):
if not i%(n+m) and i != 0:
l[i-m:i] = [0]*m
print(l)
[10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 0, 0, 10, 10, 10, 10, 10]
if not i%(n+m)
is used to check for indexes that are multiple of n+m
. Once a good index is found, replace the previous m
entries to the value you want. and i != 0
simply skip the first index since 0%X
is always 0.
edited 28 mins ago
answered 36 mins ago


alec_djinn
2,20921834
2,20921834
This is just a less efficient and less readable version of therange
answer.
– Aran-Fey
29 mins ago
I didn't look for efficiency indeed, but about readability, I disagree, I personally find this answer way more readable and easy to understand. It's a personal opinion of course.
– alec_djinn
26 mins ago
The output should be[..., 0, 0, 10, 10, 10, 10, 0]
, not[..., 0, 0, 10, 10, 10, 10, 10]
.
– klim
20 mins ago
add a comment |Â
This is just a less efficient and less readable version of therange
answer.
– Aran-Fey
29 mins ago
I didn't look for efficiency indeed, but about readability, I disagree, I personally find this answer way more readable and easy to understand. It's a personal opinion of course.
– alec_djinn
26 mins ago
The output should be[..., 0, 0, 10, 10, 10, 10, 0]
, not[..., 0, 0, 10, 10, 10, 10, 10]
.
– klim
20 mins ago
This is just a less efficient and less readable version of the
range
answer.– Aran-Fey
29 mins ago
This is just a less efficient and less readable version of the
range
answer.– Aran-Fey
29 mins ago
I didn't look for efficiency indeed, but about readability, I disagree, I personally find this answer way more readable and easy to understand. It's a personal opinion of course.
– alec_djinn
26 mins ago
I didn't look for efficiency indeed, but about readability, I disagree, I personally find this answer way more readable and easy to understand. It's a personal opinion of course.
– alec_djinn
26 mins ago
The output should be
[..., 0, 0, 10, 10, 10, 10, 0]
, not [..., 0, 0, 10, 10, 10, 10, 10]
.– klim
20 mins ago
The output should be
[..., 0, 0, 10, 10, 10, 10, 0]
, not [..., 0, 0, 10, 10, 10, 10, 10]
.– klim
20 mins ago
add a comment |Â
up vote
0
down vote
[j for i in [[input_num] * n + [0] * m for x in range(int(num / (m + n)) + 1)][:num] for j in i]
Maybe?
Result
>>> num, input_num, m, n=95, 10, 2, 3
>>> [j for i in [[input_num] * n + [0] * m for x in range(int(num / (m + n)) + 1)][:num] for j in i]
[10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0 , 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0]
2
I guess the list full of10
is just for an example...
– Julien
1 hour ago
@Julien No, OP said ' it can be any number, but it will always be the same one' so it will be full of any same number.
– Moonsik Park
50 mins ago
1
1) Your output is incorrect (it's[10, 10, 0, 0, 0]
instead of[10, 10, 10, 0, 0]
) 2) Concatenating lists withsum
is an antipattern with a massive run time complexity.
– Aran-Fey
36 mins ago
@Aran-Fey Nope, it's correct.
– Moonsik Park
31 mins ago
Oops. Indeed it is. Edited.
– Moonsik Park
26 mins ago
add a comment |Â
up vote
0
down vote
[j for i in [[input_num] * n + [0] * m for x in range(int(num / (m + n)) + 1)][:num] for j in i]
Maybe?
Result
>>> num, input_num, m, n=95, 10, 2, 3
>>> [j for i in [[input_num] * n + [0] * m for x in range(int(num / (m + n)) + 1)][:num] for j in i]
[10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0 , 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0]
2
I guess the list full of10
is just for an example...
– Julien
1 hour ago
@Julien No, OP said ' it can be any number, but it will always be the same one' so it will be full of any same number.
– Moonsik Park
50 mins ago
1
1) Your output is incorrect (it's[10, 10, 0, 0, 0]
instead of[10, 10, 10, 0, 0]
) 2) Concatenating lists withsum
is an antipattern with a massive run time complexity.
– Aran-Fey
36 mins ago
@Aran-Fey Nope, it's correct.
– Moonsik Park
31 mins ago
Oops. Indeed it is. Edited.
– Moonsik Park
26 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
[j for i in [[input_num] * n + [0] * m for x in range(int(num / (m + n)) + 1)][:num] for j in i]
Maybe?
Result
>>> num, input_num, m, n=95, 10, 2, 3
>>> [j for i in [[input_num] * n + [0] * m for x in range(int(num / (m + n)) + 1)][:num] for j in i]
[10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0 , 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0]
[j for i in [[input_num] * n + [0] * m for x in range(int(num / (m + n)) + 1)][:num] for j in i]
Maybe?
Result
>>> num, input_num, m, n=95, 10, 2, 3
>>> [j for i in [[input_num] * n + [0] * m for x in range(int(num / (m + n)) + 1)][:num] for j in i]
[10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0, 10, 10, 10, 0 , 0, 10, 10, 10, 0, 0, 10, 10, 10, 0, 0]
edited 25 mins ago


Aran-Fey
20.4k53063
20.4k53063
answered 1 hour ago
Moonsik Park
4044
4044
2
I guess the list full of10
is just for an example...
– Julien
1 hour ago
@Julien No, OP said ' it can be any number, but it will always be the same one' so it will be full of any same number.
– Moonsik Park
50 mins ago
1
1) Your output is incorrect (it's[10, 10, 0, 0, 0]
instead of[10, 10, 10, 0, 0]
) 2) Concatenating lists withsum
is an antipattern with a massive run time complexity.
– Aran-Fey
36 mins ago
@Aran-Fey Nope, it's correct.
– Moonsik Park
31 mins ago
Oops. Indeed it is. Edited.
– Moonsik Park
26 mins ago
add a comment |Â
2
I guess the list full of10
is just for an example...
– Julien
1 hour ago
@Julien No, OP said ' it can be any number, but it will always be the same one' so it will be full of any same number.
– Moonsik Park
50 mins ago
1
1) Your output is incorrect (it's[10, 10, 0, 0, 0]
instead of[10, 10, 10, 0, 0]
) 2) Concatenating lists withsum
is an antipattern with a massive run time complexity.
– Aran-Fey
36 mins ago
@Aran-Fey Nope, it's correct.
– Moonsik Park
31 mins ago
Oops. Indeed it is. Edited.
– Moonsik Park
26 mins ago
2
2
I guess the list full of
10
is just for an example...– Julien
1 hour ago
I guess the list full of
10
is just for an example...– Julien
1 hour ago
@Julien No, OP said ' it can be any number, but it will always be the same one' so it will be full of any same number.
– Moonsik Park
50 mins ago
@Julien No, OP said ' it can be any number, but it will always be the same one' so it will be full of any same number.
– Moonsik Park
50 mins ago
1
1
1) Your output is incorrect (it's
[10, 10, 0, 0, 0]
instead of [10, 10, 10, 0, 0]
) 2) Concatenating lists with sum
is an antipattern with a massive run time complexity.– Aran-Fey
36 mins ago
1) Your output is incorrect (it's
[10, 10, 0, 0, 0]
instead of [10, 10, 10, 0, 0]
) 2) Concatenating lists with sum
is an antipattern with a massive run time complexity.– Aran-Fey
36 mins ago
@Aran-Fey Nope, it's correct.
– Moonsik Park
31 mins ago
@Aran-Fey Nope, it's correct.
– Moonsik Park
31 mins ago
Oops. Indeed it is. Edited.
– Moonsik Park
26 mins ago
Oops. Indeed it is. Edited.
– Moonsik Park
26 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52493328%2fperiodically-replacing-values-in-a-list%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
How about you write a program?
– Julien
1 hour ago
Will the input list always be filled with one and the same value (like 10 in your example)? In other words, do you want to create a new list like
[10, 10, 10, 0, 0, ...]
or do you want to overwrite every nth value in an existing list?– Aran-Fey
58 mins ago
@Aran-Fey 10 is indeed just an example. But the input list will stay constant. So it can be any number, but it will always be the same one. It doesn't really matter to me if the list gets overwritten or if a new one is created.
– Riley
57 mins ago
Is the length of the list going to be the same?
– alec_djinn
53 mins ago
@alec_djinn, yes it will. It would be nice to see a more general method, but if it works for a specific length, that's also ok.
– Riley
48 mins ago