Created
January 5, 2020 23:38
-
-
Save luisenriquecorona/24eacb01fa5e1d16853e103f644bbe7a to your computer and use it in GitHub Desktop.
Demonstrates the effect of loading three dicts with the same data, just in different order. The resulting dictionaries all compare equal, even if their order is not the same.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# dial codes of the top 10 most populous countries | |
DIAL_CODES = [ | |
(86, 'China'), | |
(91, 'India'), | |
(1, 'United States'), | |
(62, 'Indonesia'), | |
(55, 'Brazil'), | |
(92, 'Pakistan'), | |
(880, 'Bangladesh'), | |
(234, 'Nigeria'), | |
(7, 'Russia'), | |
(81, 'Japan'), | |
] | |
d1 = dict(DIAL_CODES) | |
print('d1:', d1.keys()) | |
d2 = dict(sorted(DIAL_CODES)) | |
print('d2:', d2.keys()) | |
d3 = dict(sorted(DIAL_CODES, key=lambda x:x[1])) | |
print('d3:', d3.keys()) | |
assert d1 == d2 and d2 == d3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment