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

r0g aioe.org at technicalbloke.com
Mon Jan 4 22:01:52 EST 2010


Steven D'Aprano wrote:
> On Tue, 05 Jan 2010 00:52:56 +0000, r0g wrote:
> 
>> 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?


OK, I hadn't read all the other responses when I posted, some of which
make fair points why this wouldn't be wise. Fair enough.



> 
> That's not the existing behaviour. List concatenation doesn't mutate the 
> left hand list, it creates a new list:
> 


I would expect it to append. That's my prejudice though, as I do that
far more often :/



> 
>>>> L = [1, 2, 3]
>>>> L2 = L + [4, 5, 6]
>>>> L
> [1, 2, 3]
>>>> L2
> [1, 2, 3, 4, 5, 6]
> 
> 
> But if you insist on in-place modification, why do you prefer appending 
> the right hand sequence to the left instead of prepending the left hand 
> sequence to the right?
> 
> 
> 



In-place seems more natural for a mutable type. I admit the left right
thing is my prejudice though, western cultural bias I suppose. Its not
entirely unprecedented though, the parser reads left to right and the
leftmost terms take precedent in lazy logic evaluation.

Still, the responses to this have convinced me the + operator shouldn't
make assumptions, I'm more open to how += works though as it implies
in-place and the left over right precedent quite nicely.


Roger.



More information about the Python-list mailing list