Canonical conversion of dict of dicts to list of dicts
Marco Ippolito
maroloccio at gmail.com
Tue Mar 30 11:27:21 EDT 2021
> > 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? Anyway, I like that it uses simple elemental parts that
> have been around a long long time in Python.
You can check the release notes of previous Python versions:
https://docs.python.org/3/whatsnew/3.5.html
specifically:
PEP 448 - Additional Unpacking Generalizations
https://docs.python.org/3/whatsnew/3.5.html#whatsnew-pep-448
or, if you have multiple local installations of Python (or Docker or similar
technologies), test running the same code snippet with different Python
versions.
Example with Python 3.9:
```
$ docker run --rm --tty --interactive python:3.9 \
python -c 'import sys; d1 = > dict(x=1); d2 = {"y": 2, **d1}; print(sys.version_info, d2)'
sys.version_info(major=3, minor=9, micro=2, releaselevel='final', serial=0) {'y': 2, 'x': 1}
```
Example with Python 3.4:
```
$ docker run --rm --tty --interactive python:3.4 \
python -c 'import sys; d1 = > dict(x=1); d2 = {"y": 2, **d1}; print(sys.version_info, d2)'
...
SyntaxError: invalid syntax
```
More information about the Python-list
mailing list