Trying to use the code given below I get the error: row[0] = "M" if row[0] == "male" else row[0] = "F" ^^^^^^ SyntaxError: cannot assign to subscript here. Maybe you meant '==' instead of '='?
rows = []
for row in csvreader:
row[0] = "M" if row[0] == "male" else row[0] = "F"
rows.append(row)
However when I format it normally(see below) the error does not occur. Why is that?
for row in csvreader:
if row[0] == "male":
row[0] = "M"
else:
row[0] = "F"
rows.append(row)
Note: row is list of strings
Looking online at the error given it says it occurs because of an assignment to a literal, which doesn't seem to be the case.
source https://stackoverflow.com/questions/75795262/why-does-shorthand-if-assignment-to-list-not-work
Comments
Post a Comment