Dict to "flat" list of (key,value)

David C. Fox davidcfox at post.harvard.edu
Wed Jul 30 16:57:04 EDT 2003


David C. Fox wrote:

Oops - a mistake and an omission

> 4.  and then reduce that to a list of tuples with
> 
>     reduce(operator.add, lol)

You need to import the operator module first.

>     def flatten(u):
>         if isinstance(u[1], type([])):
>             return (u[1], [u[2]])
>         return map(lambda x: (u[0], x), u[1])

I got my cases reversed - that should be "if not isinstance...





More information about the Python-list mailing list