TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Dictionary update() method with Examples

Python Dictionary update() method with Examples on clear(), copy(), fromkeys(), get(), len(), items(), keys(), setdefault(), values(), ascii(), enumerate(), update(), pop() etc.

<< Back to PYTHON

Python Dictionary update() Method

Python update() method updates the dictionary with the key and value pairs. It inserts key/value if it is not present. It updates key/value if it is already present in the dictionary.

It also allows an iterable of key/value pairs to update the dictionary. like: update(a=10,b=20) etc.

Signature and examples of this method are given below.

Signature

update([other])

Parameters

other: It is a list of key/value pairs.

Return

It returns None.

Let?s see some examples of update() method to understand it?s functionality.

Python Dictionary update() Method Example 1

It is a simple example to update the dictionary by passing key/value pair. This method updates the dictionary. See an example below.

# Python dictionary update() Method
# Creating a dictionary
einventory = {'Fan': 200, 'Bulb':150, 'Led':1000}
print("Inventory:",einventory)
# Calling Method
einventory.update({'cooler':50})
print("Updated inventory:",einventory)

Output:

Inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000}
Updated inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 50}

Python Dictionary update() Method Example 2

If element (key/value) pair is already presents in the dictionary, it will overwrite it. See an example below.

# Python dictionary update() Method
# Creating a dictionary
einventory = {'Fan': 200, 'Bulb':150, 'Led':1000,'cooler':50}
print("Inventory:",einventory)
# Calling Method
einventory.update({'cooler':50})
print("Updated inventory:",einventory)
einventory.update({'cooler':150})
print("Updated inventory:",einventory)

Output:

Inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 50}
Updated inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 50}
Updated inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 150}

Python Dictionary update() Method Example 3

The update() method also allows iterable key/value pairs as parameter. See, the example below two values are passed to the dictionary and it is updated.

# Python dictionary update() Method
# Creating a dictionary
einventory = {'Fan': 200, 'Bulb':150, 'Led':1000}
print("Inventory:",einventory)
# Calling Method
einventory.update(cooler=50,switches=1000)
print("Updated inventory:",einventory)

Output:

Inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000}
Updated inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 50, 'switches': 1000}

Next TopicPython Dictionary




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf