This may be a better way of solving the problem (using or
and __setitem__
to change the content of row
). To change which item to substitute, just change the 1
in row.__setitem__(1, '0')
to whatever index you'd like.
def foo(s):
nums = [substring.split(',') for substring in s.split()];return ' '.join(','.join(row.__setitem__(1, '0') or row) for row in nums)
print foo('a,b,c d,e,f, g,h,i')
Output:
a,0,c d,0,f, g,0,i
This works since __setitem__
works in-place, and hence always returns None
. Since the first statement in row.__setitem__(1, '0') or row
is None
, the result of the expression after or
is yielded (i.e. row
, which is what we want).