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

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

  1. En Mon, 04 Jan 2010 04:58:54 -0300, Chris Rebert <>
    escribió:
    > On Sun, Jan 3, 2010 at 11:51 PM, Gabriel Genellina
    > <> 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...

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jan 4, 2010
    #1
    1. Advertising

  2. Gabriel Genellina

    r0g Guest

    Gabriel Genellina wrote:
    > En Mon, 04 Jan 2010 04:58:54 -0300, Chris Rebert <>
    > escribió:
    >> On Sun, Jan 3, 2010 at 11:51 PM, Gabriel Genellina
    >> <> 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.
     
    r0g, Jan 5, 2010
    #2
    1. Advertising

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


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


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



    --
    Steven
     
    Steven D'Aprano, Jan 5, 2010
    #3
  4. Gabriel Genellina

    r0g Guest

    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.
     
    r0g, Jan 5, 2010
    #4
    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:
    794
    Jay Parlar
    Mar 17, 2006
  2. Davy
    Replies:
    3
    Views:
    1,893
    Wildemar Wildenburger
    Nov 7, 2007
  3. Gabriel Genellina
    Replies:
    0
    Views:
    234
    Gabriel Genellina
    Jan 4, 2010
  4. Gabriel Genellina
    Replies:
    2
    Views:
    3,209
    Gabriel Genellina
    Jan 4, 2010
  5. Carlos

    Concatenate/De-Concatenate

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

Share This Page