sorting dictionary python 3

A modern and fast solution, for Python 3.7. May also work in some interpreters of Python 3.6. TLDR To sort a dictionary by keys use: sorted_dict = {k: disordered[k] for k in sorted(disordered)} Almost three times faster than the accepted answer; probably more when you include imports. Comment on the accepted answer The example in …

Read more

JavaScript dictionary with names

Another approach would be to have an array of objects, with each individual object holding the properties of a column. This slightly changes the structure of “myMappings”, but makes it easy to work with: var myMappings = [ { title: “Name”, width: “10%” }, { title: “Phone”, width: “10%” }, { title: “Address”, width: “50%” …

Read more

Immutable Scala Map implementation that preserves insertion order [duplicate]

ListMap implements an immutable map using a list-based data structure, and thus preserves insertion order. scala> import collection.immutable.ListMap import collection.immutable.ListMap scala> ListMap(1 -> 2) + (3 -> 4) res31: scala.collection.immutable.ListMap[Int,Int] = Map(1 -> 2, 3 -> 4) scala> res31 + (6 -> 9) res32: scala.collection.immutable.ListMap[Int,Int] = Map(1 -> 2, 3 -> 4, 6 -> 9) …

Read more

.NET Dictionary: get existing value or create and add new value

We have a slightly different take on this, but the effect is similar: public static TValue GetOrCreate<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key) where TValue : new() { if (!dict.TryGetValue(key, out TValue val)) { val = new TValue(); dict.Add(key, val); } return val; } Called: var dictionary = new Dictionary<string, List<int>>(); List<int> numbers = dictionary.GetOrCreate(“key”); …

Read more