Javatpoint Logo
Javatpoint Logo

How to Create a Dictionary in Python

In Python, a dictionary is an unordered sequence of data entries that can be used to record data entries in the same way that a map can. Unlike alternative data structures that only carry a singular item as an object, Dictionary contains a key: value pair. The dictionary includes a Key-Value field to improve the efficiency of this data type.

Creating a Dictionary

We can create a dictionary in Python by wrapping a collection of objects in curly brackets and segregating them with a comma. The Dictionary keeps track of a couple of entries, one part of this is called a key, and the other is called the value of the key and is referred to as the key: value pair. We can store any data type as a value for the key and give the same value to two keys, but keys should be immutable and unique.

Code

Output:

Dictionary created using curly braces: 
{1: 'Javatpoint', 2: 'Python', 3: 'Dictionary'}

Dictionary with keys of multiple data type: 
{'Website': 'Javatpoint', 3: [2, 3, 5, 'Dictionary']}

The built-in method dict() could also be used to generate a dictionary. By simply placing two curly brackets {}, a blank dictionary can be built.

Code

Output:

An empty Dictionary: 
{}

Dictionary created by using dict() method: 
{1: 'Python', 2: 'Javatpoint', 3: 'Dictionary'}

Dictionary with key:value pair format: 
{1: 'Javatpoint', 2: 'Python', 3: 'Dictionary'}

Adding Elements to a Dictionary

The insertion of items in the Python Dictionary could be done in various methods. Items can be added to the dictionary using this particular format Dictionary[Key] = 'Value'. We can use the in-built update() function to update a current key in a Dictionary. Nested key: values can also be inserted into a preexisting Dictionary. If the key-value combination is present in the dictionary, the value for that key is modified; if this is not the case, a new key is created and added to the Dictionary with the given value.

Code

Output:

The empty Dictionary: 
{}

Dictionary after addition of these elements: 
{0: 'Javatpoint', 2: 'Python', 3: 'Dictionary'}

Dictionary after addition of the list: 
{0: 'Javatpoint', 2: 'Python', 3: 'Dictionary', 'list_values': (3, 4, 6)}

Updated dictionary: 
{1: 'Javatpoint', 2: 'Python'}

After addtion of a Nested Key: 
{0: 'Javatpoint', 2: 'Tutorial', 3: 'Dictionary', 'list_values': (3, 4, 6), 5: {'Nested_key': {1: 'Nested', 2: 'Key'}}}

Removing Elements from Dictionary

Using the pop() function, we can eliminate a specific item from a dictionary. This function returns the value of the key removed.

The popitem() function removes and returns any (key, value) element pair from the given dictionary. We can use the clear() function to erase all objects in one go.

We could also use the del keyword to eliminate single items or even the entire dictionary.

Code

Output:

After removing a key using pop(): 
{1: 'a', 2: 'b', 3: 'c', 5: 'e'}

After removing an arbitrary key: 
{1: 'a', 2: 'b', 3: 'c'}

After removing all the items: 
{}
No dictionary of the given name

Python Dictionary Methods

The techniques that we can use with a dictionary are listed below. Some of these have already been mentioned in this tutorial.

Method Description
clear() Every item in the dictionary is removed.
copy() A shallow replica of the dictionary is returned.
fromkeys(seq[, v]) This function gives a new dictionary having seq's keys and v's values (defaults value for keys is None).
get(key[,d]) This function returns the value of the specific key. It returns d if the key doesn't exist.
items() It returns a new object in (key, value) format containing the dictionary's contents.
keys() Creates a new object with the keys of the dictionary.
pop(key[,d]) Eliminates the item associated with the given key and gives its value, or d if the key is not located. It raises KeyError if d is not specified and the given key is not found in the dictionary.
popitem() Eliminates and replaces a random object (key, value). If the given dictionary is empty, it raises a KeyError.
setdefault(key[,d]) If the key is found in the dictionary, it gives the associated value. If not, yields d and adds the key with the value of d. (default value is None).
update([other]) Overwrites preexisting keys in the given dictionary
values() Initializes a new dictionary object with the dictionary's elements.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA