•
•
•
•
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
![]() |
•
•
Join Date: Apr 2008
Posts: 26
Reputation:
Rep Power: 1
Solved Threads: 0
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.
Surely there's a cleaner way to do that loop? Anyone?
Sorta. It's trivial, but the code you end up with is pretty gruesome.
Python Syntax (Toggle Plain Text)
>>> nums = [1.0,2.0,3.0,4.0,5.0] >>> for n in range(len(nums)-1,-1,-1): # Same as saying "if nums[n] is odd" if nums[n] % 2 != 0: del nums[n] >>> print nums [2.0, 4.0]
Last edited by FreezeBlink : Jun 3rd, 2008 at 2:26 am.
Try a list comprehension:
Python Syntax (Toggle Plain Text)
>>> nums = [1.0,2.0,3.0,4.0,5.0] >>> evens = [x for x in nums if x%2==0] >>> evens [2.0, 4.0] >>>
•
•
Join Date: Jul 2006
Posts: 562
Reputation:
Rep Power: 4
Solved Threads: 72
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:
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:
Jeff
Python Syntax (Toggle Plain Text)
>>> nums = [1.0,2.0,3.0,4.0,5.0] >>> for n in nums[:]: if n % 2 == 1: # better way to say 'is odd' 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:
Python Syntax (Toggle Plain Text)
>>> nums = [1.0,2.0,3.0,4.0,5.0] >>> nums = [x for x in nums if x % 2 == 1]
Jeff
![]() |
•
•
•
•
•
•
•
•
DaniWeb Python Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- What's better? Windows 2000 Server or Linux Server? (Windows Servers and IIS)
- Cannot Connect to, Linksys' Web Based Utility, I need help (Networking Hardware Configuration)
- Centering a form in VB.NET (VB.NET)
- CPU at 100% after installing broadband!?! (Windows NT / 2000 / XP / 2003)
- Dedicated Server (Networking Hardware Configuration)
- Interpretation of an instructors C++ program... (C++)
- Multipointer Manipulating System and Method (Computer Science and Software Design)
- Still working !! (DaniWeb Community Feedback)
Other Threads in the Python Forum
- Previous Thread: how can i increase the visibility of dialogs and windows...
- Next Thread: stop a cycel of number


Linear Mode