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

r0g aioe.org at technicalbloke.com
Mon Jan 4 19:52:56 EST 2010


Gabriel Genellina wrote:
> En Mon, 04 Jan 2010 04:58:54 -0300, Chris Rebert <clp2 at rebertia.com>
> escribió:
>> On Sun, Jan 3, 2010 at 11:51 PM, Gabriel Genellina
>> <gagsl-py2 at yahoo.com.ar> wrote:
> 
>>> 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
> 
> Sorry, I inadvertedly posted an incomplete message. Note the last part:
> 
>>> In-place addition += does work:
>>>
>>> py> a = [1,2,3]
>>> py> a += (4,5)
>>> py> a
>>> [1, 2, 3, 4, 5]
> 
>> Given that tuples are sometimes used as a poor man's object (i.e.
>> collection of data fields), whereas lists are not typically used that
>> way, I'd say it's probably a good thing an explicit type conversion is
>> required here.
> 
> In that case += should not be allowed either...
> 


I'd be strongly inclined to think the result would be the sequence on
the left with the data from the second sequence appended to it. What's
wrong with a little duck typing here eh?

Roger.



More information about the Python-list mailing list