Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32728
| References | <afn3i8Fn4j1U1@mid.uni-berlin.de> |
|---|---|
| Date | 2012-11-04 23:06 +1100 |
| Subject | Re: surprising += for lists |
| From | Alec Taylor <alec.taylor6@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3254.1352030765.27098.python-list@python.org> (permalink) |
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 <doomster@knuut.de> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
surprising += for lists Ulrich Eckhardt <doomster@knuut.de> - 2012-11-04 12:57 +0100 Re: surprising += for lists Alec Taylor <alec.taylor6@gmail.com> - 2012-11-04 23:06 +1100 Re: surprising += for lists Dave Angel <d@davea.name> - 2012-11-04 07:45 -0500 Re: surprising += for lists Terry Reedy <tjreedy@udel.edu> - 2012-11-04 12:57 -0500
csiph-web