Tkinter

Python Dictionary

Python Dictionary

In Python, a dictionary is an unordered collection of data values that can store data values like a map. Unlike other Data Types, which can only carry a single value as an element, Dictionary can hold a key: value pair.

Dictionaries are Python's implementation of an associative array, more often known as a data structure. A dictionary is made up of a set of key-value pairs. Each key-value pair corresponds to a certain value. The dictionary includes key-value pairs to make it more efficient.

The Making of a Dictionary

A Dictionary is built in Python by enclosing a succession of entries in curly braces and separating them with a comma. Dictionary stores a pair of values, one of which is the Key and the other is the Key:value pair element. In a dictionary, values can be of any data type and replicated, but keys cannot be copied and must be immutable.

Output


The built-in function dict can also be used to generate a dictionary (). By just placing two curly brackets, an empty dictionary can be built.


Output


Nested Dictionary:


Adding to a Dictionary's Elements

The addition of elements in Python Dictionary can be accomplished in a variety of ways. By defining value together with the key, such as Dict[Key] = 'Value,' one value at a time can be added to a Dictionary. The built-in update() method can be used to replace an existing value in a Dictionary. An existing Dictionary can also have nested key values added.

Dictionary Elements to be Removed

Using the keyword del:

The del keyword can be used to delete keys in the Python Dictionary. Specific values from a dictionary and the entire dictionary can be erased using the del keyword. Items in a nested dictionary can also be deleted with the del keyword and a specified nested key and key to be deleted from that nested Dictionary.