[Tutor] Problem with if statements and else statements

Cameron Simpson cs at zip.com.au
Mon May 29 19:08:25 EDT 2017


On 29May2017 01:17, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
>On 29/05/17 00:12, Alex Kleider wrote:
>> Would
>>>>> if Month in {'January', '1'}:
>>
>> be even better?  (regarding efficiency perhaps? Trivial point, I know,
>> but just wondering.)
>
>If in doubt try it out and profile/time it.
>
>But I don't think it will make much difference since ultimately
>it still has to test each value (although a hashing algorithm
>may be involved that works on partial matches...) But if in
>doubt...

Hi Alex,

As written it should be a bit slower: to construct a set each member get tested 
for presence. The cost is in making the set, not in searching it.

_However_, supposing your program were doing this a lot. You might well have a 
global (or, better, long lived shared object) containing a set that has already 
been constructed. Then:

  if Month in the_set:

is very fast; constant time. Whereas as you would expect, checking a list is 
linear with the size of the list.

So, using a list:

  seen = []
  for item in some_item_generator():
    if item in seen:
      continue
    seen.append(item)
    ... do stuff with item, which is new ...

The cost of the "if" goes up linearly as you add more items.

Using a set:

  seen = {}
  for item in some_item_generator():
    if item in seen:
      continue
    seen.add(item)
    ... do stuff with item, which is new ...

The second version will be much more effiient as the "seen" set grows; the 
lookup time on the set is essentially O(1) (constant time).

But for an ad hoc 2 element list as in your original example the difference 
will be pretty small; making the 2 element set _should_ be slightly more 
expensive, and isn't the common idiom (==> less readable). Personally I use:

  if value in ('a', 'b', 'c'):

BTW, in Python we tend to use named like "Fred" for classes (or factories), and 
"fred" for regular variables. And "FRED" for things that would be constants in 
other languages. Eg:

  MAX_THINGS = 16

  class Foo:
    ....

  def FooBah(x):
    return Foo(x, style="bah")

  for fred in ....:

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list