Let's say I have arr = [[1], [0, 0], [2], [3], [0, 0], [4]]
Could I flatten this into [1, 0, 0, 2, 3, 0, 0, 4]
without having to use itertools, map/reduce, or a list comp? I am struggling to figure out a way to do this. Thanks.
This is what I have tried so far, it is a leetcode question: https://leetcode.com/problems/duplicate-zeros/
class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
for ix in range(len(arr) - 2):
if arr[ix] == 0:
arr[ix] = [0, 0]
del arr[-1]
else:
l = []
l.append(arr[ix])
arr[ix] = l
# Output when you return arr is [[1],[0, 0],[2],[3],[0, 0],[4]]