Issue
I wanted to replace the every 2 alternative items in the list. Example below:
a = [1, 2, 3, 9, 4, 1, 2, 2, 3, 3, 4, 2, 5, 2, 3]
Result should be:
[1, ' ' , ' ', 9, ' ', ' ', 2, ' ', ' ', 3, ' ', ' ', 5, ' ', 3]
Last and first item should not be replaced
I have for loop brute force enumerate way to do this. I am curious if there is any easy and alternative way to do this.
Solution
A simple for
loop of in-place changes would actually be the most efficient in both speed and space:
for i in range(1, len(a) - 1):
if i % 3:
a[i] = ' '
Answered By - blhsing
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.