Introduction
• A dictionaryis 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.
What is aDictionary?
• 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.
5.
What is aDictionary?
• 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.
6.
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 dictionary usingdict() function
• We can create dictionaries by using a builtin
function called dict().
10.
Adding and replacingvalues to a dictionary
• To add new items in to a dictionary , we can
use the subscript [] operator.
• Syntax: Dictioanry_name [key] = value
Formatting dictionaries
• The% operator is used to substitute values from a dictionary ,
in to a string by using key.
14.
Deleting items fromdictionary
• 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]
The len Function
•You can find the number of the items in a
dictionary by using len(dictionary). For
example.
17.
Testing Whether aKey Is in a Dictionary
• You can use the in or not in operator to
determine whether a key is in the dictionary.
For example:
18.
Equality Test (or)Comparingtwo 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.
The Dictionary Methods
•The Python class for dictionaries is dict. Figure
14.2 lists the methods that can be invoked
from a dictionary object
22.
• 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:
23.
The methods ofdictionary class
• keys() – return a sequence of keys from dictionary
24.
The methods ofdictionary class
• values() – return a sequence of vales from a
dictionary
25.
The methods ofdictionary class
• items() – Return a sequence of tuples
26.
The methods ofdictionary class
• get(key) – return the value for key.
27.
The methods ofdictionary class
• clear() – Delete all entries of a dictionary
28.
Traversing dictionaries
• Thefor 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.
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}
33.
Write a pythonprogram to calculate the polynomial
equation -2 + y^4 + 3y^6