4 ms·
Python is great, I just wish it had JS-like object destructuring applied to dicts – so convenient!
by iliaznk 8y ago
Python is great, I just wish it had JS-like object destructuring applied to dicts – so convenient!
- rataata_jr 8y agoIf you can think it, it can be done. There is probably a library for it. It is just not standard.
- iliaznk 8y agoThat I have no doubts about, but having it in the standard lib would be awesome!
- Rotareti 8y agoMe too. There is a library that solves this: https://github.com/Infinidat/munch https://github.com/Infinidat/munch from munch import Munch foo = {'a':1, 'b':2} foo = Munch(foo) print(foo.a) # 1 print(foo['a']) # 1
- jasongi 8y agoYou mean like a = {"foo": "bar", "bar": "foo"} b = {**a} You can do this since 3.5 https://www.python.org/dev/peps/pep-0448/ https://www.python.org/dev/peps/pep-0448/ You can also do dict comprehensions which is much more expressive b = {k: v for k,v in a.items() if v == "bar"} This way you can copy only the parts of a dict you want in a simple one-liner. https://www.python.org/dev/peps/pep-0274/ https://www.python.org/dev/peps/pep-0274/