surprising += for lists

Discussion in 'Python' started by Ulrich Eckhardt, Nov 4, 2012.

  1. Hi everybody!

    I was just smacked by some very surprising Python 2.7 behaviour. I was
    assembling some 2D points into a list:

    points = []
    points += (3, 5)
    points += (4, 6)

    What I would have expected is to have [(3, 5), (4, 6)], instead I got [3,
    5, 4, 6]. My interpretations thereof is that the tuple (x, y) is iterable,
    so the elements are appended one after the other. Actually, I should have
    used points.append(), but that's a different issue.

    Now, what really struck me was the fact that [] + (3, 5) will give me a
    type error. Here I wonder why the augmented assignment behaves so much
    different.

    Can anyone help me understand this?

    Thanks!

    Uli
     
    Ulrich Eckhardt, Nov 4, 2012
    #1
    1. Advertising

  2. Ulrich Eckhardt

    Alec Taylor Guest

    Quick aside, you can insert tuples without much effort: `points += ((3,5),)`

    And also that I can't do the reverse, i.e.:
    >>> foo = tuple()
    >>> foo += [5,6]

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: can only concatenate tuple (not "list") to tuple

    On Sun, Nov 4, 2012 at 10:57 PM, Ulrich Eckhardt <> wrote:
    > Hi everybody!
    >
    > I was just smacked by some very surprising Python 2.7 behaviour. I was
    > assembling some 2D points into a list:
    >
    > points = []
    > points += (3, 5)
    > points += (4, 6)
    >
    > What I would have expected is to have [(3, 5), (4, 6)], instead I got [3,
    > 5, 4, 6]. My interpretations thereof is that the tuple (x, y) is iterable,
    > so the elements are appended one after the other. Actually, I should have
    > used points.append(), but that's a different issue.
    >
    > Now, what really struck me was the fact that [] + (3, 5) will give me a
    > type error. Here I wonder why the augmented assignment behaves so much
    > different.
    >
    > Can anyone help me understand this?
    >
    > Thanks!
    >
    > Uli
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Alec Taylor, Nov 4, 2012
    #2
    1. Advertising

  3. Ulrich Eckhardt

    Dave Angel Guest

    On 11/04/2012 06:57 AM, Ulrich Eckhardt wrote:
    > Hi everybody!
    >
    > I was just smacked by some very surprising Python 2.7 behaviour. I was
    > assembling some 2D points into a list:
    >
    > points = []
    > points += (3, 5)
    > points += (4, 6)
    >
    > What I would have expected is to have [(3, 5), (4, 6)], instead I got [3,
    > 5, 4, 6].


    mylist +=
    is equivalent to mylist.extend. And as you say, what you wanted was
    append.


    > My interpretations thereof is that the tuple (x, y) is iterable,

    You're confusing cause and effect. If it weren't iterable, it'd be an
    error. It would NOT just somehow change to be equivalent to append.

    >>> points.extend(4)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'int' object is not iterable

    > so the elements are appended one after the other. Actually, I should have
    > used points.append(), but that's a different issue.
    >
    > Now, what really struck me was the fact that [] + (3, 5) will give me a
    > type error. Here I wonder why the augmented assignment behaves so much
    > different.


    What I wonder about is why list's __add__ is so fussy.

    > Can anyone help me understand this?
    >
    > Thanks!
    >
    > Uli
    >
    >

    I'd also point out that when using the extend() function call, we'd have
    to spell it:

    points.extend((3,5))

    The extra parentheses are to make it clear to the compiler that this is
    a single argument, a tuple, and not two arguments.

    And similarly,
    points.append((3,5))
    to get your original desired behavior.


    --

    DaveA
     
    Dave Angel, Nov 4, 2012
    #3
  4. Ulrich Eckhardt

    Terry Reedy Guest

    On 11/4/2012 7:45 AM, Dave Angel wrote:

    > What I wonder about is why list's __add__ is so fussy.


    Guido's reason is that it is not clear what the types of [1,2] + (3,4),
    (1,2) + [3,4], [] + range(4), range(2) + [3,4], etcetera should be. Such
    mixtures may be bugs. Seq.__add__ exists to implement '+'.

    [].extend() will clearly be a list, and accepts any iterable.

    I believe the same logic is used for set operations.

    --
    Terry Jan Reedy
     
    Terry Reedy, Nov 4, 2012
    #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. naija naija

    Help with Datagrid (Surprising No Data!!)

    naija naija, Sep 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    443
    naijacoder naijacoder
    Oct 2, 2004
  2. janek
    Replies:
    3
    Views:
    431
    Ioannis Vranos
    Feb 21, 2005
  3. Michael Olea

    A Surprising Partial Specialization

    Michael Olea, Jun 14, 2005, in forum: C++
    Replies:
    1
    Views:
    323
    Pete Becker
    Jun 14, 2005
  4. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    445
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  5. candide
    Replies:
    15
    Views:
    546
    Stephen Hansen
    Jun 18, 2010
Loading...

Share This Page