Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20181 > unrolled thread

Removing items from a list

Started byThomas Philips <tkpmep@gmail.com>
First post2012-02-10 12:04 -0800
Last post2012-02-13 05:55 -0800
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Removing items from a list Thomas Philips <tkpmep@gmail.com> - 2012-02-10 12:04 -0800
    Re: Removing items from a list Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-10 13:22 -0700
      Re: Removing items from a list Thomas Philips <tkpmep@gmail.com> - 2012-02-10 12:48 -0800
    Re: Removing items from a list MRAB <python@mrabarnett.plus.com> - 2012-02-10 20:26 +0000
    Re: Removing items from a list Chris Angelico <rosuav@gmail.com> - 2012-02-11 08:58 +1100
      Re: Removing items from a list Thomas Philips <tkpmep@gmail.com> - 2012-02-13 05:55 -0800

#20181 — Removing items from a list

FromThomas Philips <tkpmep@gmail.com>
Date2012-02-10 12:04 -0800
SubjectRemoving items from a list
Message-ID<7c4cc084-7b55-48f9-a3ac-219f33b69d0b@k10g2000yqk.googlegroups.com>
In the past, when deleting items from a list, I looped through the
list in reverse to avoid accidentally deleting items I wanted to keep.
I tried something different today, and, to my surprise, was able to
delete items correctly, regardless of the direction in which I looped,
in both Python 3.2.2. and 2..1 -  does the remove() function somehow
allow the iteration to continue correctly even when items are removed
from the midde of the list?

>>> x = list(range(10))
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in x:
	if i % 2 == 0:
		x.remove(i)

>>> x
[1, 3, 5, 7, 9]
>>> for i in reversed(x):
	if i % 2 == 0:
		x.remove(i)

>>> x
[1, 3, 5, 7, 9]
>>> x = list(range(10))
>>> for i in reversed(x):
	if i % 2 == 0:
		x.remove(i)


>>> x
[1, 3, 5, 7, 9]

Sincerely

Thomas Philips

[toc] | [next] | [standalone]


#20182

FromIan Kelly <ian.g.kelly@gmail.com>
Date2012-02-10 13:22 -0700
Message-ID<mailman.5674.1328905402.27778.python-list@python.org>
In reply to#20181
On Fri, Feb 10, 2012 at 1:04 PM, Thomas Philips <tkpmep@gmail.com> wrote:
> In the past, when deleting items from a list, I looped through the
> list in reverse to avoid accidentally deleting items I wanted to keep.
> I tried something different today, and, to my surprise, was able to
> delete items correctly, regardless of the direction in which I looped,
> in both Python 3.2.2. and 2..1 -  does the remove() function somehow
> allow the iteration to continue correctly even when items are removed
> from the midde of the list?

No.  Your test works because you never attempt to remove two adjacent
items, so the skipping of items doesn't end up mattering.  Try the
same thing, but print out the values as you iterate over them:


>>> x = list(range(10))
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in x:
...   print(i)
...   if i % 2 == 0:
...     x.remove(i)
...
0
2
4
6
8
>>> x
[1, 3, 5, 7, 9]


Had you attempted to remove any of the odd numbers as well, it would
have failed.

Cheers,
Ian

[toc] | [prev] | [next] | [standalone]


#20184

FromThomas Philips <tkpmep@gmail.com>
Date2012-02-10 12:48 -0800
Message-ID<66f4c010-31f4-4aa5-851e-48ec174c0b96@t24g2000yqj.googlegroups.com>
In reply to#20182
Thanks for the insight. I saw the behavious as soon as I extended x
with a bunch of 0's

>>> x = list(range(10))
>>> x.extend([0]*10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> for i in reversed(x):
	if i % 2 == 0:
		x.remove(i)

>>> x
[1, 3, 5, 7, 9]

>>> x = list(range(10))
>>> x.extend([0]*10)
>>> x
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> for i in x:
	if i % 2 == 0:
		x.remove(i)


>>> x
[1, 3, 5, 7, 9, 0, 0, 0, 0, 0]

[toc] | [prev] | [next] | [standalone]


#20183

FromMRAB <python@mrabarnett.plus.com>
Date2012-02-10 20:26 +0000
Message-ID<mailman.5676.1328905774.27778.python-list@python.org>
In reply to#20181
On 10/02/2012 20:04, Thomas Philips wrote:
> In the past, when deleting items from a list, I looped through the
> list in reverse to avoid accidentally deleting items I wanted to keep.
> I tried something different today, and, to my surprise, was able to
> delete items correctly, regardless of the direction in which I looped,
> in both Python 3.2.2. and 2..1 -  does the remove() function somehow
> allow the iteration to continue correctly even when items are removed
> from the midde of the list?
>
>>>>  x = list(range(10))
>>>>  x
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>>  for i in x:
> 	if i % 2 == 0:
> 		x.remove(i)
>
>>>>  x
> [1, 3, 5, 7, 9]
>>>>  for i in reversed(x):
> 	if i % 2 == 0:
> 		x.remove(i)
>
>>>>  x
> [1, 3, 5, 7, 9]
>>>>  x = list(range(10))
>>>>  for i in reversed(x):
> 	if i % 2 == 0:
> 		x.remove(i)
>
>
>>>>  x
> [1, 3, 5, 7, 9]
>
The answer is no. For example:

 >>> for i in x:
	print("i is", i)
	if i % 2 == 0:
		x.remove(i)

		
i is 0
i is 1
i is 2
i is 4
 >>> x
[0, 1, 3, 5]

[toc] | [prev] | [next] | [standalone]


#20193

FromChris Angelico <rosuav@gmail.com>
Date2012-02-11 08:58 +1100
Message-ID<mailman.5680.1328911138.27778.python-list@python.org>
In reply to#20181
On Sat, Feb 11, 2012 at 7:04 AM, Thomas Philips <tkpmep@gmail.com> wrote:
> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>> for i in x:
>        if i % 2 == 0:
>                x.remove(i)

Just a quickie, is there a reason you can't use a list comprehension?

x = [i for i in x if i % 2]

ChrisA

[toc] | [prev] | [next] | [standalone]


#20345

FromThomas Philips <tkpmep@gmail.com>
Date2012-02-13 05:55 -0800
Message-ID<306adc3f-e452-48ed-9be0-2656248f738c@m5g2000yqk.googlegroups.com>
In reply to#20193
I could indeed have addressed this problem with a list comprehension.
It escaped me at the time because the larger problem I was trying to
solve included removing data from a dictionary:

    months =
sorted(list(dataDict.keys()))                                  #Sort
months in ascending order

    for mth in
reversed(months):                                            #Remove
months with inadequate data
        if len(dataDict[mth]) < minItems:
           months.remove(mth)
           del dataDict[mth]

There's more than one way to solve this problem, and, with the benefit
of hindsight, my solution was sloppy, but thanks to the help I
received, I was able to get my code working correctly. Cleaning up is
the next phase!

Thanks, all.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web