Dictionary to tuple

Robert Kern rkern at ucsd.edu
Tue Jun 28 11:37:24 EDT 2005


Erik Max Francis wrote:
> bruno modulix wrote:
> 
>>Err... don't you spot any useless code here ?-)
>>
>>(tip: dict.items() already returns a list of (k,v) tuples...)
> 
> But it doesn't return a tuple of them.  Which is what the tuple call 
> there does.

The useless code referred to was the list comprehension.

 >>> t = tuple([(k,v) for k,v in d.iteritems()])

versus

 >>> t = tuple(d.items())

or even

 >>> t = tuple(d.iteritems())

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the Python-list mailing list