Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42147 > unrolled thread
| Started by | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
|---|---|
| First post | 2013-03-28 15:25 +0000 |
| Last post | 2013-03-28 15:54 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
list comprehension misbehaving Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-28 15:25 +0000
Re: list comprehension misbehaving duncan smith <buzzard@invalid.invalid> - 2013-03-28 15:54 +0000
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
|---|---|
| Date | 2013-03-28 15:25 +0000 |
| Subject | list comprehension misbehaving |
| Message-ID | <mailman.3887.1364484337.2939.python-list@python.org> |
Dear all, with
a=list(range(1,11))
why (in Python 2.7 and 3.3) is this explicit for loop working:
for i in a[:-1]:
a.pop() and a
giving:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
[1, 2, 3]
[1, 2]
[1]
but the equivalent comprehension failing:
[a.pop() and a for i in a[:-1]]
giving:
[[1], [1], [1], [1], [1], [1], [1], [1], [1]]
???
Especially, since these two things *do* work as expected:
[a.pop() and a[:] for i in a[:-1]]
[a.pop() and print(a) for i in a[:-1]] # Python 3 only
Thanks for your help,
Wolfgang
[toc] | [next] | [standalone]
| From | duncan smith <buzzard@invalid.invalid> |
|---|---|
| Date | 2013-03-28 15:54 +0000 |
| Message-ID | <515467b1$0$65353$862e30e2@ngroups.net> |
| In reply to | #42147 |
On 28/03/13 15:25, Wolfgang Maier wrote: > Dear all, with > a=list(range(1,11)) > > why (in Python 2.7 and 3.3) is this explicit for loop working: > for i in a[:-1]: > a.pop() and a > > giving: > [1, 2, 3, 4, 5, 6, 7, 8, 9] > [1, 2, 3, 4, 5, 6, 7, 8] > [1, 2, 3, 4, 5, 6, 7] > [1, 2, 3, 4, 5, 6] > [1, 2, 3, 4, 5] > [1, 2, 3, 4] > [1, 2, 3] > [1, 2] > [1] > > but the equivalent comprehension failing: > [a.pop() and a for i in a[:-1]] > > giving: > [[1], [1], [1], [1], [1], [1], [1], [1], [1]] > > ??? > Especially, since these two things *do* work as expected: > [a.pop() and a[:] for i in a[:-1]] > [a.pop() and print(a) for i in a[:-1]] # Python 3 only > > Thanks for your help, > Wolfgang > With the for loop the list is printed each time you pop an element. With the list comprehension all but one of the elements are popped before the string representation of the resulting list (containing several references to a) is printed. The two list comprehensions that you say "work" behave differently because the first contains copies of a (which are unaffected by subsequent pops), and the second because (I imagine) it does something similar to, [a.pop() and repr(a) for i in a[:-1]] on 2.7 (I haven't migrated to 3 yet). i.e. The list contains a representation of a after each element is popped. Duncan
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web