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

Discussion in 'Python' started by Gabriel Genellina, Jan 4, 2010.

  1. 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
     
    Gabriel Genellina, Jan 4, 2010
    #1
    1. Advertising

  2. On Mon, 04 Jan 2010 04:59:02 -0300, Gabriel Genellina wrote:

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



    > py> [1,2,3] + (4,5)



    What result are you expecting? A list or a tuple?



    > Traceback (most recent call last):
    > 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
    error as this:

    >>> 2 + "2"

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for +: 'int' and 'str'



    > In-place addition += does work:
    >
    > 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
    method:



    >>> a += "abc" # same as a.extend("abc")
    >>> a

    [1, 2, 3, 4, 5, 'a', 'b', 'c']
    >>> a += {None: -1}
    >>> a

    [1, 2, 3, 4, 5, 'a', 'b', 'c', None]



    --
    Steven
     
    Steven D'Aprano, Jan 4, 2010
    #2
    1. Advertising

  3. En Mon, 04 Jan 2010 05:22:44 -0300, Steven D'Aprano
    <> escribió:

    > On Mon, 04 Jan 2010 04:59:02 -0300, Gabriel Genellina wrote:
    >
    >> 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...

    >> In-place addition += does work:
    >>
    >> 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
    > method:
    >
    >>>> a += "abc" # same as a.extend("abc")
    >>>> a

    > [1, 2, 3, 4, 5, 'a', 'b', 'c']
    >>>> 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
     
    Gabriel Genellina, Jan 4, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Parlar
    Replies:
    0
    Views:
    813
    Jay Parlar
    Mar 17, 2006
  2. Davy
    Replies:
    3
    Views:
    1,916
    Wildemar Wildenburger
    Nov 7, 2007
  3. Gabriel Genellina
    Replies:
    0
    Views:
    252
    Gabriel Genellina
    Jan 4, 2010
  4. Gabriel Genellina
    Replies:
    3
    Views:
    450
  5. Carlos

    Concatenate/De-Concatenate

    Carlos, Oct 12, 2012, in forum: VHDL
    Replies:
    10
    Views:
    959
Loading...

Share This Page