4 ms·
The tuple is sequence-like enough to be concatenated with a list: []+(2,) differs from []+[2] only by the type of the temporary object that contains 2.
by HelloNurse 4y ago
The tuple is sequence-like enough to be concatenated with a list: []+(2,) differs from []+[2] only by the type of the temporary object that contains 2.
- klyrs 4y agoThe truly weird thing here is that [] + (2,) is a TypeError: can only concatenate list (not "tuple") to list but L = [] L += (2,) is totally cool. In the end, it makes sense because I can't tell you if []+() should be a tuple or a list, and even if I did, I might have a different answer for ()+[]; whereas L+=bla looks like the type of L should not change.
- maleldil 4y agoThey're different operations. The first is concatenation; the second is extension. Concatenation only works with other lists, but extension works with any iterable.