my_dict={'a':'One','b':'Two','c':'Three'}
my_dict2={'d':'Four'} # new key
my_dict.update(my_dict2)
print(my_dict)
Output is here
{'a': 'One', 'b': 'Two', 'c': 'Three', 'd': 'Four'}
my_dict={'a':'One','b':'Two','c':'Three'}
my_dict2={'b':'New value'} # existing key
my_dict.update(my_dict2)
print(my_dict)
Output (key b with New value is updated )
{'a': 'One', 'b': 'New value', 'c': 'Three'}
my_dict={'a':'One','b':'Two','c':'Three'}
my_dict.update(d='Four',e='Five')
print(my_dict)
Output
{'a': 'One', 'b': 'Two', 'c': 'Three', 'd': 'Four', 'e': 'Five'}
my_dict={'a':'One','b':'Two','c':'Three'}
my_dict.update(b='New one',d='Four',e='Five')
print(my_dict)
Output ( value of key b is updated and new keys d and e added )
{'a': 'One', 'b': 'New one ', 'c': 'Three', 'd': 'Four', 'e': 'Five'}
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.