Dictionaries
Introduction
• A dictionary is a container object that stores a collection of key/value
pairs. It enables fast retrieval, deletion, and updating of the value by using
the key.
• A dictionary is an efficient data structure for such a task.
• A dictionary is a collection that A dictionary is an efficient data structure
for such a task.
• A dictionary is a collection that stores the values along with the keys.
• The keys are like an index operator. In a list, the indexes are integers.
• In a dictionary, the key must be a hashable object. A dictionary cannot
contain duplicate keys.
• A dictionary is also known as a map, which maps each key to a value.
• Each key maps to one value. A key and its corresponding value form an
item (or entry) stored in a dictionary, as shown in Figure 14.1a.
Contd..
What is a Dictionary?
• Dictionary in Python is an unordered collection of data
values, used to store data values like a map, which unlike
other Data Types that hold only single value as an element.
• Dictionary holds a pair of values, one being the Key and the
other corresponding pair element being its key-value.
What is a Dictionary?
• Dictionary in Python is an unordered collection of
data values, used to store data values like a map,
which unlike other Data Types that hold only single
value as an element.
• A dictionary is a collection which is unordered,
changeable and indexed
• Values in a dictionary can be of any data type and
can be duplicated, whereas keys can’t be repeated
and must be immutable.
• Key value is provided in the dictionary to make it
more optimized.
Creating a Dictionary
• n Python, a Dictionary can be created by
placing sequence of elements within curly
“{ }” braces, separated by ‘comma’.
• Dictionary keys are case sensitive, same name
but different cases of Key will be treated
distinctly.
Creating empty dictionary
Creating dictionary using dict() function
• We can create dictionaries by using a builtin
function called dict().
Adding and replacing values to a dictionary
• To add new items in to a dictionary , we can
use the subscript [] operator.
• Syntax: Dictioanry_name [key] = value
Adding and replacing values to a dictionary
Replacing value of dictionary
Formatting dictionaries
• The % operator is used to substitute values from a dictionary ,
in to a string by using key.
Deleting items from dictionary
• We can delete any entry from dictionary.
• The “del” operator is used to remove key and
its associated value.
• If key is found in the dictionary then it is
removed otherwise python raises an error.
• Syntax: del dictioanry_name[key]
Deleting items from dictionary
The len Function
• You can find the number of the items in a
dictionary by using len(dictionary). For
example.
Testing Whether a Key Is in a Dictionary
• You can use the in or not in operator to
determine whether a key is in the dictionary.
For example:
Equality Test (or)Comparing two dictionaries
• The “==” operator is used to test if two
dictionaries contains the same items.
• It returns true two dictionaries contains same
items.
• Similarly the “!=” operator is used to test if
two dictionaries doesn’t contain same items.
• It returns true two dictionaries contains
different items.
example
Comparing two dictionaries
The Dictionary Methods
• The Python class for dictionaries is dict. Figure
14.2 lists the methods that can be invoked
from a dictionary object
• The get(key) method is similar to dictionaryName[key] except that the get
method returns None if the key is not in the dictionary rather than raising
an exception.
• The pop(key) method is the same as del dictionaryName[key]. Here are
some examples that show these methods in use:
The methods of dictionary class
• keys() – return a sequence of keys from dictionary
The methods of dictionary class
• values() – return a sequence of vales from a
dictionary
The methods of dictionary class
• items() – Return a sequence of tuples
The methods of dictionary class
• get(key) – return the value for key.
The methods of dictionary class
• clear() – Delete all entries of a dictionary
Traversing dictionaries
• The for loop is used to traverse all keys and
values of a dictionary.
• The variable of the for loop is bound to each
key in an unspecified order.
• It means it retrieve the keys and values in any
order.
Traversing dictionaries using for loop
Nested dictionaries
• A dictionary with in a dictionary is called nested dictionary.
Traversing Nested dictionaries
Polynomials as dictionaries
• Dictionaries can change their content so they are mutable
in nature.
• The keys in a dictionary is not restricted to be strings.
• Any immutable object can be used as key.
• A common type of key used in dictionaries is integers.
• We can use dictionaries while calculating polynomial
equation.
• A dictionary is used to map a power to a coefficient.
let the equation be -2 + y^4 + 3y^6
The dictionary for the above equation is
{0:-2, 4:1, 6:3}
Write a python program to calculate the polynomial
equation -2 + y^4 + 3y^6
Dictionariesasdfghjsdfghjklsdfghjkl.pptx