Why would I use `dict.update()` in Python instead of assigning values directly?

I understand that the update() method on dictionaries in Python can be used to add new key-value pairs or update existing ones. For example:

my_dict = {'1': 11, '2': 1445}
my_dict.update({'1': 645, 5: 123})

This will result in:

{'1': 645, '2': 1445, 5: 123}

But I can also achieve the same effect by directly assigning values:

my_dict['1'] = 645
my_dict[5] = 123

This also gives:

{'1': 645, '2': 1445, 5: 123}

So I’m wondering, in what situations is python dict update crucial or more beneficial than just assigning values manually? Are there practical cases where update() is the preferred approach?

I usually use update() when I have multiple key-value pairs to add or modify at once. It keeps your code clean instead of writing several assignments:

my_dict.update({'a': 1, 'b': 2, 'c': 3})

Much neater than doing my_dict[‘a’]=1; my_dict[‘b’]=2; …

Another case: merging dictionaries. If you have two dictionaries, update() is perfect:

dict1.update(dict2)

All keys from dict2 are added or overwrite existing ones. Doing this with individual assignments would be cumbersome.

From my experience, update() is handy for dynamic updates, like updating configuration dictionaries from user input or files. It’s safer and more readable than looping through keys manually.