Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue on page /book/pure_python/dictionaries.html #4

Open
rameshm76 opened this issue Nov 2, 2024 · 0 comments
Open

Issue on page /book/pure_python/dictionaries.html #4

rameshm76 opened this issue Nov 2, 2024 · 0 comments

Comments

@rameshm76
Copy link

Firstly, Amazing work on this book.

The merging of maps need more attention:

Consider d1 with "year" is having a different value than d2

d1 = {"version": "3.11", "language": "Python", "year": 2991}

Then the results vary a bit when duplicate keys exists on both sides.

First one win:
d7 = ChainMap(d1, d2)
d8 = dict(d2, **d1)

Last one win:
d1 |= d2
d3 = d1 | d2
d6 = dict(list(d1.items()) + list(d2.items()))
d1.update(d2.copy())
d5 = {**d1, **d2}

To deliver consistent results:
d7 = ChainMap(d2, d1)
d8 = dict(d1, **d2)

I liked ChainMap as it is clear which one takes precedence

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant