TypeError: can only concatenate list (not "tuple") to list

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Jan 4 10:27:48 CET 2010


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 you
> 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...

-- 
Gabriel Genellina




More information about the Python-list mailing list