Discussion:
TypeError: can only concatenate list (not "tuple") to list
Gabriel Genellina
2010-01-04 07:51:40 UTC
Permalink
This
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
--
Gabriel Genellina
Chris Rebert
2010-01-04 07:58:54 UTC
Permalink
On Sun, Jan 3, 2010 at 11:51 PM, Gabriel Genellina
Post by Gabriel Genellina
This
py> [1,2,3] + (4,5)
?File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
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.

Cheers,
Chris
--
http://blog.rebertia.com
Gabriel Genellina
2010-01-04 07:59:02 UTC
Permalink
Is there any reason for this error? Apart from "nobody cared to write the
code"

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]
--
Gabriel Genellina
David Williams
2010-01-04 08:20:44 UTC
Permalink
Post by Gabriel Genellina
Is there any reason for this error? Apart from "nobody cared to write the
code"
py> [1,2,3] + (4,5)
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
py> a = [1,2,3]
py> a += (4,5)
py> a
[1, 2, 3, 4, 5]
--
Gabriel Genellina
--
http://mail.python.org/mailman/listinfo/python-list
They are different types.
David Williams
2010-01-04 08:24:56 UTC
Permalink
Post by Gabriel Genellina
Is there any reason for this error? Apart from "nobody cared to write the
code"
py> [1,2,3] + (4,5)
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
py> a = [1,2,3]
py> a += (4,5)
py> a
[1, 2, 3, 4, 5]
--
Gabriel Genellina
--
http://mail.python.org/mailman/listinfo/python-list
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?
Gabriel Genellina
2010-01-04 09:27:48 UTC
Permalink
En Mon, 04 Jan 2010 05:24:56 -0300, David Williams <david at bibliolabs.com>
Post by David Williams
Post by Gabriel Genellina
py> [1,2,3] + (4,5)
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
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
Jean-Michel Pichavant
2010-01-04 13:36:25 UTC
Permalink
Post by Gabriel Genellina
En Mon, 04 Jan 2010 05:24:56 -0300, David Williams
Post by David Williams
Post by Gabriel Genellina
py> [1,2,3] + (4,5)
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
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...
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
very smart.
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.

JM
Steven D'Aprano
2010-01-04 09:45:46 UTC
Permalink
Post by Gabriel Genellina
En Mon, 04 Jan 2010 05:24:56 -0300, David Williams
Post by David Williams
Post by Gabriel Genellina
py> [1,2,3] + (4,5)
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
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.
If you can't explain it, it's probably a bad idea. Why should lists be
privileged over tuples?

How does it work? Do tuples automatically turn into lists, or does the
left hand value over-ride the right hand value?

In other words, would you expect:


"1" + 1 = "11"
1 + "1" = 2

or would you expect:


"1" + 1 = "11"
1 + "1" = "11"
Post by Gabriel Genellina
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...
Yes, you should be a little more careful in your tests.
Post by Gabriel Genellina
Post by David Williams
Post by Gabriel Genellina
2 + 3.5
5.5
Post by Gabriel Genellina
Post by David Williams
Post by Gabriel Genellina
3.5 + 2
5.5


Nothing to do with left versus right.
--
Steven
Gabriel Genellina
2010-01-04 08:27:59 UTC
Permalink
En Mon, 04 Jan 2010 04:58:54 -0300, Chris Rebert <clp2 at rebertia.com>
Post by Chris Rebert
On Sun, Jan 3, 2010 at 11:51 PM, Gabriel Genellina
Post by Gabriel Genellina
py> [1,2,3] + (4,5)
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
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...
--
Gabriel Genellina
r0g
2010-01-05 00:52:56 UTC
Permalink
Post by Gabriel Genellina
En Mon, 04 Jan 2010 04:58:54 -0300, Chris Rebert <clp2 at rebertia.com>
Post by Chris Rebert
On Sun, Jan 3, 2010 at 11:51 PM, Gabriel Genellina
Post by Gabriel Genellina
py> [1,2,3] + (4,5)
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
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.
Steven D'Aprano
2010-01-05 02:35:03 UTC
Permalink
Post by r0g
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?
That's not the existing behaviour. List concatenation doesn't mutate the
Post by r0g
L = [1, 2, 3]
L2 = L + [4, 5, 6]
L
[1, 2, 3]
Post by r0g
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?
--
Steven
r0g
2010-01-05 03:01:52 UTC
Permalink
Post by Steven D'Aprano
Post by r0g
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.
Post by Steven D'Aprano
That's not the existing behaviour. List concatenation doesn't mutate the
I would expect it to append. That's my prejudice though, as I do that
far more often :/
Post by Steven D'Aprano
Post by r0g
L = [1, 2, 3]
L2 = L + [4, 5, 6]
L
[1, 2, 3]
Post by r0g
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.

Steven D'Aprano
2010-01-04 08:22:44 UTC
Permalink
Post by Gabriel Genellina
Is there any reason for this error? Apart from "nobody cared to write
the code"
Yes, because such implicit conversions would be a bad idea.
Post by Gabriel Genellina
py> [1,2,3] + (4,5)
What result are you expecting? A list or a tuple?
Post by Gabriel Genellina
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "tuple") to list
Apart from the different error message, this is essentially the same
Post by Gabriel Genellina
2 + "2"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Post by Gabriel Genellina
py> a = [1,2,3]
py> a += (4,5)
py> a
[1, 2, 3, 4, 5]
I call that an impressive gotcha. I believe that is because in-place
addition of lists is implemented as functionally equivalent to the extend
Post by Gabriel Genellina
a += "abc" # same as a.extend("abc")
a
[1, 2, 3, 4, 5, 'a', 'b', 'c']
Post by Gabriel Genellina
a += {None: -1}
a
[1, 2, 3, 4, 5, 'a', 'b', 'c', None]
--
Steven
Gabriel Genellina
2010-01-04 09:39:45 UTC
Permalink
En Mon, 04 Jan 2010 05:22:44 -0300, Steven D'Aprano
Post by Steven D'Aprano
Post by Gabriel Genellina
Is there any reason for this error? Apart from "nobody cared to write
the code"
Yes, because such implicit conversions would be a bad idea.
I'm slowly convincing myself that it was actually a bad idea...
Post by Steven D'Aprano
Post by Gabriel Genellina
py> a = [1,2,3]
py> a += (4,5)
py> a
[1, 2, 3, 4, 5]
I call that an impressive gotcha. I believe that is because in-place
addition of lists is implemented as functionally equivalent to the extend
Post by Gabriel Genellina
a += "abc" # same as a.extend("abc")
a
[1, 2, 3, 4, 5, 'a', 'b', 'c']
Post by Gabriel Genellina
a += {None: -1}
a
[1, 2, 3, 4, 5, 'a', 'b', 'c', None]
So += and extend are completely permissive - they slurp whatever comes
from iterating their right operand. Totally unexpected in some cases, as
in your examples above...
--
Gabriel Genellina
Loading...