Dictionary merge by updating but not overwriting if value exists

Just switch the order: z = dict(d2.items() + d1.items()) By the way, you may also be interested in the potentially faster update method. In Python 3, you have to cast the view objects to lists first: z = dict(list(d2.items()) + list(d1.items())) If you want to special-case empty strings, you can do the following: def mergeDictsOverwriteEmpty(d1, … Read more

Why is one string greater than the other when comparing strings in JavaScript?

Because, as in many programming languages, strings are compared lexicographically. You can think of this as a fancier version of alphabetical ordering, the difference being that alphabetic ordering only covers the 26 characters a through z. This answer is in response to a java question, but the logic is exactly the same. Another good one: … Read more

Efficient way to compare version strings in Java [duplicate]

Requires commons-lang3-3.8.1.jar for string operations. /** * Compares two version strings. * * Use this instead of String.compareTo() for a non-lexicographical * comparison that works for version strings. e.g. “1.10”.compareTo(“1.6”). * * @param v1 a string of alpha numerals separated by decimal points. * @param v2 a string of alpha numerals separated by decimal points. … Read more