12.Dictionary In Python
What is Dictionary?
Python Dictionary is used to store the data in a key-value pair format. The dictionary is the data type in Python, which can simulate the real-life data arrangement where some specific value exists for some particular key. It is the mutable data-structure. The dictionary is defined into element Keys and values.
Keys must be an immutable element.
Value can be any type such as list, tuple, integer, etc.
In other words, we can say that a dictionary is the collection of key-value pairs where the value can be any Python object. In contrast, the keys are the immutable Python object, i.e., Numbers, string, or tuple.
A Dictionary is enclosed under curly braces {}. For Example:
In [1]:
In above dictionary , Key 'name' holds 'Abhi' value .
Keys Must be immutable:
Keys in Dictionary must be immutable elements like numbers , strings , tuple etc. It will throw an error if key is a mutable element.
Properties of a Dictionary :
Ordered :
Dictionaries do not have index numbers but still it has keys, hence dictionaries are ordered. For Example:
In [2]:
Mutable:
Dictionaries are mutable or changeable. We can change values of dictionary elements with the help of keys. For Example :
In [3]:
Does not allow duplicate Keys:
If there is more than one key-value pair with same key , Dictionary will store only last inserted Key-Value pair. For Example:
In [5]:
Functions used in Dictionaries :
Functions like max() , min() and len() works on sequence .
In [6]:
In [7]:
In [9]:
How to access values from a dictionary :
We can access values of dictionary with the help of keys , same as we do with the help of index in list.
In [10]:
How to change values in a dictionary :
To change value of a dictionary we will simply assign new value to its key. For Example:
In [11]:
How to add values in a Dictionary :
We can add values in a dictionary simply by assigning a new value in a new key. For Example :
In [12]:
Dictionary Methods :
Functions that are specific to dictionary are known as dictionary methods. We will go through each method one by one.
keys() :
keys() method return a sequence of keys. For example :
In [13]:
values():
values() method return a sequence of values. For Example:
In [14]:
items() :
items() method returns tuple of key-value pairs in a sequence. For Example:
In [15]:
Iteration in keys of dictionaries:
Below example is to show you how to iterate keys of dictionaries :
In [16]:
We can also iterate keys of dictionaries simply iterating dictionary only. For Example:
In [17]:
Iteration in values of dictionaries:
Iteration of key-value pair or items in dictionaries:
Below example is to show you how to iterate key-value pair or items in dictionaries . It returns a tuple.
In [19]:
We can also separate keys and values from with the help of this code.
In [22]:
How to create dictionary with user inputs:
To create a dictionary with user inputs , we may ask keys and values from user and add them in an empty dictionary. For Example:
In [23]:
how to add an iterables items in a dictionaries: update()
update() method adds items (key value pair) from an iterable in a dictionary. For Example:
In [24]:
You can also update items from list or tuple or sets in a dictionary. But the only condition is it should be in key value form. A simple list cannot be converted in a dictionary. For Example :
In [2]:
hence we can only update items of an iterable that are in key value pair. So how can a list items be updated in dictionary.
What will happen if i convert a set items in a list using dict() constructor.
In [3]:
Now , above list is in form of key value pair. if we want to convert list items in a dictionary , our list should have tuples as list items with key value pair in it. For Example:
In [4]:
Remove items from Dictionary :
There are various methods to remove items from dictionary. We will go through each method step by step.
pop() method :
Removes the element with the specified key.
The pop() method removes the specified item from the dictionary.
The value of the removed item is the return value of the pop() method, see example below.
In [8]:
popitem() :
The popitem() method removes the item that was last inserted into the dictionary.
The removed item is the return value of the popitem() method, as a tuple, see example below.
In [9]:
setdefault :
Returns the value of the specified key. If the key does not exist: insert the key, with the specified value.
Syntax : dictionary.setdefault(keyname, value)
In [11]:
In above code we have set default value of role is 'not known' . So if role key does not exist in dictionary or is removed. default key value pair will be added in dictionary. For Example.
In [12]:
clear() :
clear method clears all the data in dictionary. for example:
In [13]:
copy() :
copy() method is used to copy contents of a dictionary to another dictioary.For Example:
In [14]:
dict():
dict() constructor is used to convert a sequence in a dictionary. for Example:
In [7]:
Assignment:
1.Write a Python script to merge two Python dictionaries 2.Write a Python program to iterate over dictionaries using for loops. 3.Write a Python program to remove a key from a dictionary. 4.Write a Python program to sort a given dictionary by key. 5.Write a Python program to get the maximum and minimum value in a dictionary. 6.Write a Python program to check multiple keys exists in a dictionary.
Last updated