Exploiting non-strict dictionary rules

Prior to Python 3.6, a dictionary's keys had no defined order. If we try to create a dictionary with multiple items that share a common key value, there will only be one item in the resulting dict object. There was no definition for which of the duplicated key values will be preserved, and if we've designed the algorithm properly, it shouldn't matter.

The following result is typical. The last value has replaced any prior values. Prior to Python 3.6, there was no guarantee this would happen:

>>> {'a': 1, 'a': 2}{'a': 2}

Here's a situation where we explicitly don't care which of the duplicated keys is preserved. If we look at a degenerate case of the max() function, it simply picks the largest of two values: ...

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.