<div dir="ltr">In python, we have beautiful unpacking:<div>a, b, c = [1,2,3]<br></div><div><br></div><div>and even</div><div>a, b, *c = [1,2,3,4,5]</div><div><br></div><div>We also have dictionary destructing for purposes of keywords:</div><div>myfunc(**mydict)</div><div><br></div><div>You can currently unpack a dictionary, but its almost certainly not what you would intend. </div><div><span style="font-family:monospace">a, b, c <span style="color:rgb(98,98,98)">=</span> {<span style="color:rgb(175,0,0)">'a'</span>: <span style="color:rgb(98,98,98)">1</span>, <span style="color:rgb(175,0,0)">'c'</span>: <span style="color:rgb(98,98,98)">3</span>, <span style="color:rgb(175,0,0)">'b'</span>: <span style="color:rgb(98,98,98)">2</span>}<span style="color:rgb(98,98,98)">.</span>values()<br></span></div><div><span style="font-family:monospace"><br></span></div><div>In python 3.6+ this is better since the dictionary is insertion-ordered, but is still not really what one would probably want.</div><div><br></div><div>It would be cool to have a syntax that would unpack the dictionary to values based on the names of the variables. Something perhaps like:</div><div><br></div><div>a, b, c = **mydict</div><div><br></div><div>which would assign the values of the keys 'a', 'b', 'c' to the variables. </div><div>The problem with this approach is that it only works if the key is also a valid variable name. Another syntax could potentially be used to specify the keys you care about (and the order). Perhaps:</div><div><br></div><div>a, b, c = **mydict('a', 'b', 'c')</div><div><br></div><div>I dont really like that syntax, but it gives a good idea. </div><div><br></div><div>One way to possibly achieve this today without adding syntax support could be simply adding a builtin method to the dict class:</div><div><br></div><div>a, b, c = mydict.unpack('a', 'b', 'c')</div><div><br></div><div><br></div><div>The real goal of this is to easily get multiple values from a dictionary. The current ways of doing this are:</div><div><br></div><div>a, b, c, = mydict['a'], mydict['b'], mydict['c']</div><div>or </div><div>a = mydict['a']</div><div>b = mydict['b']</div><div>c = mydict['c']</div><div><br></div><div>The later seams to be more common. Both are overly verbose in my mind.</div><div><br></div><div>One thing to consider however is the getitem vs get behavior. mydict['a'] would raise a KeyError if 'a' wasnt in the dict, whereas mydict.get('a') would return a "default" (None if not specified). Which behavior is chosen? </div><div>Maybe there is no clean solutions, but those are my thoughts. Anyone have feedback/ideas on this?</div><div><br></div><div>Nick</div><div><br></div></div>