I have a nested list and I want to make a function to convert times (with the specific format "XX:XX") into the string "time". I'm curious as to why my first example works but not my second. Why must I clone for each column to save the list comp's result? Lists are mutable, so shouldn't I be able to just save the row's result in place?
Doesn't work
def timeConvert2(schedule):
for eachClass in schedule:
eachClass = ["time" if x[2] == ':' else x for x in eachClass]
return schedule
timeConvert([["abc", "09:09", "10:10"], ["def", "11:11", "12:12"]])
=> [["abc", "09:09", "10:10"], ["def", "11:11", "12:12"]]
Works
def timeConvert1(schedule):
for eachClass in schedule:
eachClass[:] = ["time" if x[2] == ':' else x for x in eachClass]
return schedule
timeConvert([["abc", "09:09", "10:10"], ["def", "11:11", "12:12"]])
=> [["abc", "time", "time"], ["def", "time", "time"]]
I'm expecting the first example I gave to work, but the original list isn't changing...