Convert for loop into list comprehension with assignment?

I am trying to convert a for loop with an assignment into a list comprehension.

More precisely I am trying to only replace one element from a list with three indexes.

Can it be done?

for i in range(len(data)):
data[i][0] =  data[i][0].replace('+00:00','Z').replace(' ','T')

Best

Answer

If you really, really want to convert it to a list comprehension, you could try something like this, assuming the sub-lists have three elements, as you stated in the questions:

new_data = [[a.replace('+00:00','Z').replace(' ','T'), b, c] for (a, b, c) in data]

Note that this does not modify the existing list, but creates a new list, though. However, in this case I’d just stick with a regular for loop, which much better conveys what you are actually doing. Instead of iterating the indices, you could iterate the elements directly, though:

for x in data:
    x[0] =  x[0].replace('+00:00','Z').replace(' ','T')