Canonical conversion of dict of dicts to list of dicts

Chris Angelico rosuav at gmail.com
Tue Mar 30 08:22:56 EDT 2021


On Tue, Mar 30, 2021 at 11:21 PM Jon Ribbens via Python-list
<python-list at python.org> wrote:
>
> On 2021-03-30, Chris Angelico <rosuav at gmail.com> wrote:
> > On Tue, Mar 30, 2021 at 11:01 PM Jon Ribbens via Python-list
> ><python-list at python.org> wrote:
> >>
> >> On 2021-03-30, Chris Angelico <rosuav at gmail.com> wrote:
> >> > I dunno about "canonical", but here's how I'd do it:
> >> >
> >> > lod = [info | {"name": name} for name, info in dod.items()]
> >> >
> >> > You could use {"name":name}|info instead if you prefer to have the
> >> > name show up first in the dictionary.
> >>
> >> It's probably worth noting this method requires Python 3.9.
> >
> > True, and if you need 3.8 support, then the dict constructor with one
> > kwarg is the way to do it. But this way has the flexibility that you
> > can choose which way to resolve conflicts (if there's a name inside
> > the info dict, should it override the key, or not?).
>
> Python 3 point, er, 8, yeah, that's the only other version people might
> be using...
>
> <glances at Ubuntu 14.04 servers, not yet end-of-life, with Python 3.4>

Heh. I meant "3.8 or earlier" but abbreviated it a bit too much :D

<types "python" and hits tab twice... 2.7 and everything from 3.4 to 3.10>

ChrisA


More information about the Python-list mailing list