Skip to Content
Functional Python Programming - Second Edition
book

Functional Python Programming - Second Edition

by Steven F. Lott
April 2018
Intermediate to advanced content levelIntermediate to advanced
408 pages
10h 42m
English
Packt Publishing
Content preview from Functional Python Programming - Second Edition

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: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Python Programming - Third Edition

Functional Python Programming - Third Edition

Steven F. Lott

Publisher Resources

ISBN: 9781788627061Supplemental Content