How to concatenate two dictionaries to create a new one? [duplicate]

Slowest and doesn’t work in Python3: concatenate the items and call dict on the resulting list: $ python -mtimeit -s’d1={1:2,3:4}; d2={5:6,7:9}; d3={10:8,13:22}’ \ ‘d4 = dict(d1.items() + d2.items() + d3.items())’ 100000 loops, best of 3: 4.93 usec per loop Fastest: exploit the dict constructor to the hilt, then one update: $ python -mtimeit -s’d1={1:2,3:4}; d2={5:6,7:9}; … Read more

How to initialize a dict with keys from a list and empty value in Python?

dict.fromkeys directly solves the problem: >>> dict.fromkeys([1, 2, 3, 4]) {1: None, 2: None, 3: None, 4: None} This is actually a classmethod, so it works for dict-subclasses (like collections.defaultdict) as well. The optional second argument, which defaults to None, specifies the value to use for the keys. Note that the same object will be … Read more

Case insensitive access for generic dictionary

There’s no way to specify a StringComparer at the point where you try to get a value. If you think about it, “foo”.GetHashCode() and “FOO”.GetHashCode() are totally different so there’s no reasonable way you could implement a case-insensitive get on a case-sensitive hash map. You can, however, create a case-insensitive dictionary in the first place … Read more

What is the right way to treat Python argparse.Namespace() as a dictionary?

You can access the namespace’s dictionary with vars(): >>> import argparse >>> args = argparse.Namespace() >>> args.foo = 1 >>> args.bar = [1,2,3] >>> d = vars(args) >>> d {‘foo’: 1, ‘bar’: [1, 2, 3]} You can modify the dictionary directly if you wish: >>> d[‘baz’] = ‘store me’ >>> args.baz ‘store me’ Yes, it … Read more

Declare and initialize a Dictionary in Typescript

Edit: This has since been fixed in the latest TS versions. Quoting @Simon_Weaver’s comment on the OP’s post: Note: this has since been fixed (not sure which exact TS version). I get these errors in VS, as you would expect: Index signatures are incompatible. Type ‘{ firstName: string; }’ is not assignable to type ‘IPerson’. … Read more

Java – How to create new Entry (key, value)

There’s public static class AbstractMap.SimpleEntry<K,V>. Don’t let the Abstract part of the name mislead you: it is in fact NOT an abstract class (but its top-level AbstractMap is). The fact that it’s a static nested class means that you DON’T need an enclosing AbstractMap instance to instantiate it, so something like this compiles fine: Map.Entry<String,Integer> … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)