Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Removing items from a list
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Thomas Philips  
View profile  
 More options Feb 10 2012, 3:04 pm
Newsgroups: comp.lang.python
From: Thomas Philips <tkp...@gmail.com>
Date: Fri, 10 Feb 2012 12:04:34 -0800 (PST)
Local: Fri, Feb 10 2012 3:04 pm
Subject: Removing items from a list
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Kelly  
View profile  
 More options Feb 10 2012, 3:22 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Fri, 10 Feb 2012 13:22:47 -0700
Local: Fri, Feb 10 2012 3:22 pm
Subject: Re: Removing items from a list

On Fri, Feb 10, 2012 at 1:04 PM, Thomas Philips <tkp...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Feb 10 2012, 3:26 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Fri, 10 Feb 2012 20:26:26 +0000
Local: Fri, Feb 10 2012 3:26 pm
Subject: Re: Removing items from a list
On 10/02/2012 20:04, Thomas Philips wrote:

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]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Philips  
View profile  
 More options Feb 10 2012, 3:48 pm
Newsgroups: comp.lang.python
From: Thomas Philips <tkp...@gmail.com>
Date: Fri, 10 Feb 2012 12:48:49 -0800 (PST)
Local: Fri, Feb 10 2012 3:48 pm
Subject: Re: Removing items from a list
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]

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Angelico  
View profile  
 More options Feb 10 2012, 4:58 pm
Newsgroups: comp.lang.python
From: Chris Angelico <ros...@gmail.com>
Date: Sat, 11 Feb 2012 08:58:55 +1100
Local: Fri, Feb 10 2012 4:58 pm
Subject: Re: Removing items from a list

On Sat, Feb 11, 2012 at 7:04 AM, Thomas Philips <tkp...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Philips  
View profile  
 More options Feb 13 2012, 8:55 am
Newsgroups: comp.lang.python
From: Thomas Philips <tkp...@gmail.com>
Date: Mon, 13 Feb 2012 05:55:41 -0800 (PST)
Local: Mon, Feb 13 2012 8:55 am
Subject: Re: Removing items from a list
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »