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


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

Re: For-each behavior while modifying a collection

Started byChris Angelico <rosuav@gmail.com>
First post2013-11-29 20:47 +1100
Last post2013-11-29 20:47 +1100
Articles 1 — 1 participant

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: For-each behavior while modifying a collection Chris Angelico <rosuav@gmail.com> - 2013-11-29 20:47 +1100

#60761 — Re: For-each behavior while modifying a collection

FromChris Angelico <rosuav@gmail.com>
Date2013-11-29 20:47 +1100
SubjectRe: For-each behavior while modifying a collection
Message-ID<mailman.3395.1385718481.18130.python-list@python.org>
On Fri, Nov 29, 2013 at 9:14 AM, Valentin Zahnd <v.zahnd@gmail.com> wrote:
>      def keepByValue(self, key=None, value=[]):
>          tmpFlows = []
>          while len(self.flows) > 0:
>              row = self.flows.pop()
>              if row[key] in value:
>                  tmpFlows.append(row)
>          self.flows = tmpFlows

This is almost certainly going to be less efficient than the
comprehension - it churns the length of the list terribly. There's
very little duplication, as the actual _contents_ will not be
duplicated, only references to them; so the list comp is both clean
and efficient.

More importantly, the way you write a Python program should be:

1) Make it work and look clean.
2) See if it's fast enough. If so - and it usually will be - you're done.
3) Profile it and find out where the slow bits really are.
4) Try to speed up some of the slow bits.

You'll seldom get past step 2. The difference between "fast" and "fast
enough" is usually not visible to a human.

ChrisA

[toc] | [standalone]


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


csiph-web