Re: Removing items from a list

2012-02-13 Thread Thomas Philips
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

Re: Removing items from a list

2012-02-11 Thread Vincent Vande Vyvre
Le 10/02/12 21:48, Thomas Philips a écrit : 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,

Re: Removing items from a list

2012-02-10 Thread Chris Angelico
On Sat, Feb 11, 2012 at 7:04 AM, Thomas Philips 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 -- http://mail.python.org/mailm

Re: Removing items from a list

2012-02-10 Thread Thomas Philips
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

Re: Removing items from a list

2012-02-10 Thread MRAB
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 direct

Re: Removing items from a list

2012-02-10 Thread Ian Kelly
On Fri, Feb 10, 2012 at 1:04 PM, 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, regar

Removing items from a list

2012-02-10 Thread Thomas Philips
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.

Re: Removing items from a list simultaneously

2009-04-20 Thread Steven D'Aprano
if I wanted to return each index that was one away > from the midpoint in a, I would get [3,5]. Do you really need to remove them? Removing items from a list tends to be slow. The more Pythonic approach is to create a new list: >>> a = [1,2,3,4,5,6,7] >>> b = a[1::2] # o

Removing items from a list simultaneously

2009-04-20 Thread Ross
Is there a quick way to simultaneously pop multiple items from a list? For instance if i had the list a = [1,2,3,4,5,6,7] and I wanted to return every odd index into a new list, my output would be new_list = [2,4,6] or similarly if I wanted to return each index that was one away from the midpoint i