You could just do:
"to: {0} from: {1}".format(str(new)[1:-1] if new else "EMPTY",
str(old)[1:-1] if old else "EMPTY")
All empty containers, including []
, evaluate False
, so you don't need to explicitly check the len()
. str()
will convert the list to a string (e.g. "[1, 2, 3]"
) then the slice [1:-1]
takes all but the first character ('['
) and the last (']'
).
You can do the same thing with %
, but it is deprecated:
"to: %s from: %s" % (str(new)[1:-1] if new else "EMPTY",
str(old)[1:-1] if old else "EMPTY")
Note: this uses Python's default list
display, which puts spaces after the commas. If you really can't live with that, you could do:
"to: %s from: %s" % (str(new)[1:-1].replace(" ", "") if new else "EMPTY",
str(old)[1:-1].replace(" ", "") if old else "EMPTY")