Are ValueTuples suitable as dictionary keys?

Yes, that’s fine. The ValueTuple<…> family is a well-defined set of regular structs with the correct equality and hash-code behaviour to work as dictionary keys. There is a slight caveat in that they are mutable rather than immutable, but that doesn’t really impact them in this context thanks to copy semantics (which means: you can’t … Read more

How to convert defaultdict of defaultdicts [of defaultdicts] to dict of dicts [of dicts]?

You can recurse over the tree, replacing each defaultdict instance with a dict produced by a dict comprehension: def default_to_regular(d): if isinstance(d, defaultdict): d = {k: default_to_regular(v) for k, v in d.items()} return d Demo: >>> from collections import defaultdict >>> factory = lambda: defaultdict(factory) >>> defdict = factory() >>> defdict[‘one’][‘two’][‘three’][‘four’] = 5 >>> defdict … Read more

STL allows duplicate pairs?

The second insert with the same key is a no-op. It simply returns an iterator pointing to the existing element. std::map::insert() has a return value, which you should check. It is of type std::pair<iterator,bool>. The second element of the pair tells you whether the element has been inserted, or whether there was already an existing … Read more

How to convert Python dataclass to dictionary of string literal?

You can use dataclasses.asdict: from dataclasses import dataclass, asdict class MessageHeader(BaseModel): message_id: uuid.UUID def dict(self): return {k: str(v) for k, v in asdict(self).items()} If you’re sure that your class only has string values, you can skip the dictionary comprehension entirely: class MessageHeader(BaseModel): message_id: uuid.UUID dict = asdict

Why can’t I access elements with operator[] in a const std::map?

at() is a new method for std::map in C++11. Rather than insert a new default constructed element as operator[] does if an element with the given key does not exist, it throws a std::out_of_range exception. (This is similar to the behaviour of at() for deque and vector.) Because of this behaviour it makes sense for … Read more

What’s the difference between [] and {} vs list() and dict()? [closed]

In terms of speed, it’s no competition for empty lists/dicts: >>> from timeit import timeit >>> timeit(“[]”) 0.040084982867934334 >>> timeit(“list()”) 0.17704233359267718 >>> timeit(“{}”) 0.033620194745424214 >>> timeit(“dict()”) 0.1821558326547077 and for non-empty: >>> timeit(“[1,2,3]”) 0.24316302770330367 >>> timeit(“list((1,2,3))”) 0.44744206316727286 >>> timeit(“list(foo)”, setup=”foo=(1,2,3)”) 0.446036018543964 >>> timeit(“{‘a’:1, ‘b’:2, ‘c’:3}”) 0.20868602015059423 >>> timeit(“dict(a=1, b=2, c=3)”) 0.47635635255323905 >>> timeit(“dict(bar)”, setup=”bar=[(‘a’, 1), (‘b’, … Read more

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