User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 429,996 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,448 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 267 | Replies: 5
Reply
Join Date: Apr 2008
Posts: 26
Reputation: FreezeBlink is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
FreezeBlink FreezeBlink is offline Offline
Light Poster

Surely there's a better way?

  #1  
Jun 3rd, 2008
Iterating over a list, possibly deleting elements. It's the bane of my existence. Suppose, for instance, we have a list of numbers, and want to iterate over it and delete each and every odd one. Sounds simple, right?

Sorta. It's trivial, but the code you end up with is pretty gruesome.
  1. >>> nums = [1.0,2.0,3.0,4.0,5.0]
  2. >>> for n in range(len(nums)-1,-1,-1):
  3. # Same as saying "if nums[n] is odd"
  4. if nums[n] % 2 != 0:
  5. del nums[n]
  6.  
  7.  
  8. >>> print nums
  9. [2.0, 4.0]
Surely there's a cleaner way to do that loop? Anyone?
Last edited by FreezeBlink : Jun 3rd, 2008 at 2:26 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Posts: 257
Reputation: BearofNH is on a distinguished road 
Rep Power: 2
Solved Threads: 19
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz in Training

Re: Surely there's a better way?

  #2  
Jun 3rd, 2008
Try a list comprehension:
  1. >>> nums = [1.0,2.0,3.0,4.0,5.0]
  2. >>> evens = [x for x in nums if x%2==0]
  3. >>> evens
  4. [2.0, 4.0]
  5. >>>
Reply With Quote  
Join Date: Jun 2008
Posts: 38
Reputation: slate is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 6
slate slate is offline Offline
Light Poster

Re: Surely there's a better way?

  #3  
Jun 3rd, 2008
Can you tell me, why it is needed to delete from the list?

Is it not good enough to construct another list with the desired members, and assign it to the original list?
Reply With Quote  
Join Date: Apr 2008
Posts: 26
Reputation: FreezeBlink is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
FreezeBlink FreezeBlink is offline Offline
Light Poster

Re: Surely there's a better way?

  #4  
Jun 3rd, 2008
Because if you create a new list and assign it to the old one, you're creating an un-needed variable.
Reply With Quote  
Join Date: Jun 2008
Posts: 38
Reputation: slate is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 6
slate slate is offline Offline
Light Poster

Re: Surely there's a better way?

  #5  
Jun 3rd, 2008
how about:

del nums[1::2]
Reply With Quote  
Join Date: Jul 2006
Posts: 562
Reputation: jrcagle is on a distinguished road 
Rep Power: 4
Solved Threads: 72
jrcagle jrcagle is offline Offline
Posting Pro

Re: Surely there's a better way?

  #6  
Jun 4th, 2008
If you are determined to iterate through the original list ... and there are times when that's appropriate ... then you must iterate through a copy of the list:

  1. >>> nums = [1.0,2.0,3.0,4.0,5.0]
  2. >>> for n in nums[:]:
  3. if n % 2 == 1: # better way to say 'is odd'
  4. nums.remove(n)

By iterating through the copy, you avoid the problem of trying to delete from a list under iteration.

But in general, I endorse BearofNH's solution as the cleanest. It's scarcely more expensive to use a list comprehension than to iteration through a list to begin with, and a new variable need not be created:

  1. >>> nums = [1.0,2.0,3.0,4.0,5.0]
  2. >>> nums = [x for x in nums if x % 2 == 1]

Jeff
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 12:53 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC