Passing a dictionary to a function as keyword parameters
Figured it out for myself in the end. It is simple, I was just missing the ** operator to unpack the dictionary So my example becomes: d = dict(p1=1, p2=2) def f2(p1,p2): print p1, p2 f2(**d)
Figured it out for myself in the end. It is simple, I was just missing the ** operator to unpack the dictionary So my example becomes: d = dict(p1=1, p2=2) def f2(p1,p2): print p1, p2 f2(**d)
In Python 3.x and 2.x you can use use list to force a copy of the keys to be made: for i in list(d): In Python 2.x calling keys made a copy of the keys that you could iterate over while modifying the dict: for i in d.keys(): But note that in Python 3.x this … Read more
Map.keys() returns a MapIterator object which can be converted to Array using Array.from: let keys = Array.from( myMap.keys() ); // [“a”, “b”] EDIT: you can also convert iterable object to array using spread syntax let keys =[ …myMap.keys() ]; // [“a”, “b”]
Firstly, you can del other things besides local variables del list_item[4] del dictionary[“alpha”] Both of which should be clearly useful. Secondly, using del on a local variable makes the intent clearer. Compare: del foo to foo = None I know in the case of del foo that the intent is to remove the variable from … Read more
Note that best practice in Python 2.7 is to use new-style classes (not needed with Python 3), i.e. class Foo(object): … Also, there’s a difference between an ‘object’ and a ‘class’. To build a dictionary from an arbitrary object, it’s sufficient to use __dict__. Usually, you’ll declare your methods at class level and your attributes … Read more
Values do not necessarily have to be unique, so you have to do a lookup. You can do something like this: var myKey = types.FirstOrDefault(x => x.Value == “one”).Key; If values are unique and are inserted less frequently than read, then create an inverse dictionary where values are keys and keys are values.
My first thought was that the JSON serializer is probably pretty good at nested dictionaries, so I’d cheat and use that: >>> import json >>> print(json.dumps({‘a’:2, ‘b’:{‘x’:3, ‘y’:{‘t1’: 4, ‘t2’:5}}}, … sort_keys=True, indent=4)) { “a”: 2, “b”: { “x”: 3, “y”: { “t1”: 4, “t2”: 5 } } }
Don’t forget that map keeps its elements ordered. If you can’t give that up, obviously you can’t use unordered_map. Something else to keep in mind is that unordered_map generally uses more memory. map just has a few house-keeping pointers, and memory for each object. Contrarily, unordered_map has a big array (these can get quite big … Read more
There are dictionary comprehensions in Python 2.7+, but they don’t work quite the way you’re trying. Like a list comprehension, they create a new dictionary; you can’t use them to add keys to an existing dictionary. Also, you have to specify the keys and values, although of course you can specify a dummy value if … Read more
You can do orig.update(extra) or, if you don’t want orig to be modified, make a copy first: dest = dict(orig) # or orig.copy() dest.update(extra) Note that if extra and orig have overlapping keys, the final value will be taken from extra. For example, >>> d1 = {1: 1, 2: 2} >>> d2 = {2: ‘ha!’, … Read more