[Patches] [ python-Patches-575224 ] dict(seqn, value)

noreply@sourceforge.net noreply@sourceforge.net
Mon, 01 Jul 2002 11:08:23 -0700


Patches item #575224, was opened at 2002-06-29 01:00
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=575224&group_id=5470

Category: Core (C code)
Group: Python 2.3
Status: Open
Resolution: None
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Guido van Rossum (gvanrossum)
Summary: dict(seqn, value)

Initial Comment:
Have the dict() constructor accept a pair of 
arguments, a sequence of keys and a constant value. 
Addresses the common task of initializing dictionary 
elements to a constant value. Useful for building fast 
membership tests and for quickly (C-speed) 
eliminating duplicates in a sequence.  Is faster, more 
flexible, and clearer than:
   d = {}
   map(d.__setitem__, seqn, [])

Examples:
  uniq = dict(seqn,True).keys()  # eliminate duplicates
  termwords = dict('End Quit Stop Abort'.split(), True)
  if lexeme in termwords:  sys.exit(0)
  absences = dict('Tom Dick Harry'.split(), 0)

Patch includes source, docs, and unittest.  Also 
includes a minor change to shlex.py showing how the 
builtin can cleanly update existing code to achieve an 
order of magnitude performance boost (classifying 
characters is the most common operation in shlex).

Summary of discussion on py-dev:
At Walter and Barry's suggestion, the value was 
allowed to take any value (I initially used None). At 
Tim's suggestion, I went to an explicit two argument 
form to avoid ambiguity. If we ever get sets, Timbot 
thinks that they ought to be the tool of choice for two 
of the above use cases. Jack Jansen likes the tool 
and wants to go further and warn of inefficient 
searching when 'in' is used with sequences giving O(n) 
search speed when the could have O(1). The F/bot 
and Steve Holden poked at me for proposing 
something (speed and clarity aside) that can already 
be handled using existing constructs and Dave 
Abrahams disagreed with them.


----------------------------------------------------------------------

>Comment By: Fredrik Lundh (effbot)
Date: 2002-07-01 20:08

Message:
Logged In: YES 
user_id=38376

The "obvious" other way to use a 2-argument to dict() would 
be dict(d.keys(), d.values()).  Not sure what's more common, 
though...

(and for the record, I'd prefer a separate "set" 
type/constructor, even if it's basically just a dict without some 
of the methods)

</F>

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2002-06-30 21:47

Message:
Logged In: YES 
user_id=80475

I'm away from the computer for the next five weeks.  Oren 
Tirosh will champion this patch from here forward.  He 
can lead the discussion and made any requested 
modifications.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=575224&group_id=5470