TypeError: can only concatenate list (not "tuple") to list
jeanmichel at sequans.com
Mon Jan 4 14:36:25 CET 2010
Gabriel Genellina wrote:
> En Mon, 04 Jan 2010 05:24:56 -0300, David Williams
> <david at bibliolabs.com> escribió:
>>> py> [1,2,3] + (4,5)
>>> Traceback (most recent call last):
>>> File "<stdin>", line 1, in <module>
>>> TypeError: can only concatenate list (not "tuple") to list
>>> In-place addition += does work:
>>> py> a = [1,2,3]
>>> py> a += (4,5)
>>> py> a
>>> [1, 2, 3, 4, 5]
>> I guess to expand a bit more on what I said... What should the
>> result be?
>> A list or a tuple? The reason += works is because the end result is
>> clear; a list. But it is ambiguous in the case of concatenation: did
>> want a tuple or a list?
> Uhm... it seems "obvious" to me that [1,2,3] + (4,5) should be
> [1,2,3,4,5]. A list. That's what I would expect, although I cannot
> explain why is it *so* obvious to me.
> Given that 2 + 3.5, and 'abc' + u'def' both return an instance of
> their right operand's type, I should probably revise my preconceptions...
Haa... The well known 'obviousness theorem' :o) by which everything that
I say becomes true. Best theorem ever.
I'm really surprised that s = [1,2,3] ; s += (4,5) works fine.
Semantically, it is adding 2 operands of different type, this is not
As variales have no predefined type and can change over statements, it
would be unwise to assume that s += (4,5) should produce a list.
More information about the Python-list