l = list()
l = l + "string" #throws error
l += "string" #works
I was just wondering why this strange behaviour since we were taught l = l + "something" is the same as l += "something".
l = list()
l = l + "string" #throws error
l += "string" #works
I was just wondering why this strange behaviour since we were taught l = l + "something" is the same as l += "something".
+ requires a list when adding to a list.
+= only requires an iterable when updating a list. A str value is (for better or worse) an iterable of single-character strings, so that
l += "string"
causes 's', 't', 'r', etc to be appended to l. The += operator is more or less equivalent to
l.extend("string")