Re: Canonical conversion of dict of dicts to list of dicts

2021-03-31 Thread Greg Ewing
On 31/03/21 7:37 pm, dn wrote: Python offers mutable (can be changed) and immutable (can't) objects (remember: 'everything is an object'): https://docs.python.org/3/reference/datamodel.html?highlight=mutable%20data While that's true, it's actually irrelevant to this situation. $ a = "bob"

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread dn via Python-list
On 31/03/2021 19.24, Loris Bennett wrote: > dn writes: > >> On 31/03/2021 01.22, Loris Bennett wrote: >>> Jon Ribbens writes: On 2021-03-30, Loris Bennett wrote: > If I have dict of dicts, say > > dod = { > "alice": > { > "lang": "python", >>

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Loris Bennett
dn writes: > On 31/03/2021 01.22, Loris Bennett wrote: >> Jon Ribbens writes: >>> On 2021-03-30, Loris Bennett wrote: If I have dict of dicts, say dod = { "alice": { "lang": "python", "level": "expert" },

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread dn via Python-list
On 31/03/2021 01.22, Loris Bennett wrote: > Jon Ribbens writes: >> On 2021-03-30, Loris Bennett wrote: >>> If I have dict of dicts, say >>> >>> dod = { >>> "alice": >>> { >>> "lang": "python", >>> "level": "expert" >>> }, >>> "bob": >>> { >>>

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Marco Ippolito
> > For completeness, from 3.5 onwards, you can also do the following: > > [{'name': n, **d} for n, d in dod.items()] > Reading through these, personally I like this one best. I'm curious what > about it was enabled in 3.5? Was **kwarg expansion inside a dict literal not > possible before then? Any

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Chris Angelico
On Wed, Mar 31, 2021 at 1:56 AM Travis Griggs wrote: > > > > On Mar 30, 2021, at 12:11, Stestagg wrote: > > > > For completeness, from 3.5 onwards, you can also do the following: > > > > [{'name': n, **d} for n, d in dod.items()] > > > > Reading through these, personally I like this one best. I'm

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Travis Griggs
> On Mar 30, 2021, at 12:11, Stestagg wrote: > > For completeness, from 3.5 onwards, you can also do the following: > > [{'name': n, **d} for n, d in dod.items()] > Reading through these, personally I like this one best. I'm curious what about it was enabled in 3.5? Was **kwarg expansion in

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Loris Bennett
Jon Ribbens writes: > On 2021-03-30, Loris Bennett wrote: >> If I have dict of dicts, say >> >> dod = { >> "alice": >> { >> "lang": "python", >> "level": "expert" >> }, >> "bob": >> { >> "lang": "perl", >> "level": "noob" >>

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Chris Angelico
On Tue, Mar 30, 2021 at 11:21 PM Jon Ribbens via Python-list wrote: > > On 2021-03-30, Chris Angelico wrote: > > On Tue, Mar 30, 2021 at 11:01 PM Jon Ribbens via Python-list > > wrote: > >> > >> On 2021-03-30, Chris Angelico wrote: > >> > I dunno about "canonical", but here's how I'd do it: > >>

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Jon Ribbens via Python-list
On 2021-03-30, Chris Angelico wrote: > On Tue, Mar 30, 2021 at 11:01 PM Jon Ribbens via Python-list > wrote: >> >> On 2021-03-30, Chris Angelico wrote: >> > I dunno about "canonical", but here's how I'd do it: >> > >> > lod = [info | {"name": name} for name, info in dod.items()] >> > >> > You cou

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Stestagg
For completeness, from 3.5 onwards, you can also do the following: [{'name': n, **d} for n, d in dod.items()] On Tue, Mar 30, 2021 at 1:06 PM Chris Angelico wrote: > On Tue, Mar 30, 2021 at 11:01 PM Jon Ribbens via Python-list > wrote: > > > > On 2021-03-30, Chris Angelico wrote: > > > I dunn

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Chris Angelico
On Tue, Mar 30, 2021 at 11:01 PM Jon Ribbens via Python-list wrote: > > On 2021-03-30, Chris Angelico 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

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Jon Ribbens via Python-list
On 2021-03-30, Chris Angelico 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 thi

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Jon Ribbens via Python-list
On 2021-03-30, Loris Bennett wrote: > If I have dict of dicts, say > > dod = { > "alice": > { > "lang": "python", > "level": "expert" > }, > "bob": > { > "lang": "perl", > "level": "noob" > } > } > > is there a canonic

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Chris Angelico
On Tue, Mar 30, 2021 at 10:41 PM Loris Bennett wrote: > > Hi, > > If I have dict of dicts, say > > dod = { > "alice": > { > "lang": "python", > "level": "expert" > }, > "bob": > { > "lang": "perl", > "level": "noob" > }

Re: Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Stestagg
I'm not certain this is the clearest possible code pattern to use, but depending on the structure of your larger code, it's possible to do this, and the compactness may help with understandability (that's a judgement call!): [dict(d, name=n) for n, d in dod.items()] On Tue, Mar 30, 2021 at 12:42

Canonical conversion of dict of dicts to list of dicts

2021-03-30 Thread Loris Bennett
Hi, If I have dict of dicts, say dod = { "alice": { "lang": "python", "level": "expert" }, "bob": { "lang": "perl", "level": "noob" } } is there a canonical, or more pythonic, way of converting the outer key to a val