You are on page 1of 37

CONTENTS

( Learning Outcomes ) 
DICTIONARY in Python
Introduction :Definition, Creation, Accessing elements of a dictionary, add an
item, modify an item in a dictionary;Traversal,
Functions/Methods – len(), dict(), keys(), values(), items(), get(), update(),
del(), del, clear(), fromkeys(), copy(), pop(), popitem(), setdefault(), max(),
min(), sorted() ;
Suggested programs :
• count the number of times each character appears in a given string
using a
dictionary,
• create
and display
a dictionary
them. with employee no., names of employees, their
salary
DICTIONARY
INTRODUCTION
DICTIONARY: Definition
CONTENTS Dictionary data type is an Let, us imagine that there are
Introduction : unordered set of comma-separated some set of lockers (/Dictionary),
Definition, key: value pairs, within { } , with the Each locker, with a unique KEY
Creation, requirement that within a dictionary, and some set of things (value) in
Accessing elements, no two keys can be the same (i.e. it, is a Key:Value pair.
Add an item,
Modify an item, there are unique keys within a
Traversal, dictionary).
Membership operator We can change dictionary
Functions/Methods : We can keep different things
elements. and change the items of the locker
len(), dict(), keys(),  It can contain values of
values(), items(), get(), Locker can hold any thing viz.
update(), del(), del, mixed data dresses, books, files etc.
clear(), types.
fromkeys(), copy(), pop(), Value
= { 10 : ' CS' , ' IP': 50
popitem(), setdefault(),
max(), min(), sorted()
copy();
D
Suggested programs
}
Key
DICTIONARY: Creation
CONTENTS To create a Dictionary, we have to keep certain points in our mind:
 A KEY can only be of IMMUTABLE data type viz.
Introduction :  String
Definition,  Number
 Tuple
Creation,  VALUE can be of both MUTABLE or IMMUTABLE data type.
Accessing elements, The curly brackets { } marks the beginning and end of a
Add an item, Dictionary.
Modify an item,
 Each entry (Key:Value) consists of a pair separated by
Traversal,
Membership operator colon :
Functions/Methods that is , the key comes first and then after : its
: len(), dict(), keys(), corresponding value.
values(), items(), get(),  The key:value pairs are separated by commas,
update(), del(), del, Internally, Dictionaries are indexed (arranged on the basis of
clear(), fromkeys(), keys).
copy(), pop(), Dictionaries
Creating are alsowith
known as Associative Arrays or by
empty dictionary Creating empty dictionary
popitem(), setdefault(),
Mappings ormethod
dict() Hashes using empty curly braces {}
max(), min(), sorted()
copy();
Suggested programs
DICTIONARY: Creation contd..
Creating Creating Dictionary by initializing directly. Writing key:
dictionary value pair separated by comma , within curly braces
from {}
empty
Dictionary

Creating Dictionary from Key and Value Pairs in the form of sequences
Creating Dictionary using aTUPLE OF LISTS Creating Dictionary using a LIST OF LISTS containing
containing [key,value] pair using dict() method [key,value] pair using dict() method

Creating Dictionary using a TUPLE OF TUPLES


Creating Dictionary using a LIST OF TUPLES
containing (key,value) pair using dict()
containing (key,value) pair using dict() method
method

Creating Dictionary by zipping two Lists /or two Tuples containing Key and Values using zip( ) method and
sending the zipped sequences of key and values to the dict() method as parameter.
Accessing elements in DICTIONARY
CONTENTS
Introduction : Elements can be accessed
Definition, using get() method with
Creation,
Dictionary or directly by
Accessing writing the key inside []
with the Dictionary name
elements,
Add an item,
Modify an item,
Traversal,
Membership
operator
Functions/
Methods
len(), dict(),
keys(),
values(),
items(), get(),
update(), del(),
del,
clear(),
Add an item in a DICTIONARY
CONTENTS Adding element by writing
Introduction : Dictionaryname[key]=value
Definition, For eg. here, 4,5,6 are the
Creation, keys and ‘h’,’o’,’n’ are the
values being added by
Accessing pairing respectively
elements,
Add
an
item,
Modify an item,
Traversal,
Membership operator
Functions/ Here, for key ‘AGE’ value
Methods : is 20 and for the key
len(), dict(), keys(), 100 value is ‘Marks’
values(), items(), get(),
update(), del(), del,
clear(),
Modify an item in DICTIONARY
CONTENTS
Introduction :
Definition,
Creation,
Accessing elements,
Add an item,
Modify an
item,
Traversal,
Membership operator
Functions/
Methods :
len(), dict(), keys(),
values(), items(), get(),
update(), del(), del,
clear(),
fromkeys(), copy(), pop(),
popitem(), setdefault(),
max(), min(), sorted()
Traversal
CONTENTS
in a DICTIONARY
Introduction :
Definition,
Creation,
Accessing elements,
Add an item, OUTPUT
Modify an item,
Traversal,
Membership operator
Functions/Methods :
len(), dict(), keys(), OUTPUT
values(), items(), get(),
update(), del(), del,
clear(), fromkeys(), copy(),
pop(), popitem(),
setdefault(), max(), min(),
sorted() copy(); OUTPUT
Suggested programs :
UNDERSTANDING TRAVERSAL
in Loop, List, Tuple, Dictionary
Traversal in a Traversal in a Traversal in
LOOP LIST and TUPLE DICTIONARY

Here in Dictionary, i ,
Here in List and
variable of the loop
Tuple, i , variable
Here in Loops, i , variable of the loop picks
picks the keys and D[i]
of the loop picks the the values of the picks the values of
values of the range elements/items. the Dictionary.
MEMBERSHIP OPERATOR in a DICTIONARY
Membership operator IN, NOT IN tests for Existence of a Key in a
CONTENTS Dictionary. Returns Boolean True or False
Introduction : OPERATOR DESCRIPTION
Definition,
Creation, in Results to True , if it finds the KEY in the DICTIONARY,
else it returns False
Accessing elements,
Add an item,
Modify an item,
not in Results to True , if it DOES NOT find the KEY in the
DICTIONARY, else it returns False
Traversal,
Membership
operator
Functions/Methods

len(), dict(), keys(),
values(), items(), get(),
update(), del(), del,
clear(), fromkeys(), copy(),
pop(), popitem(),
setdefault(), max(), min(),
sorted();
Suggested programs
FUNCTIONS /
METHODS
DICTIONARY
FUNCTIONS IN DICTIONARY: len()
CONTENTS
Introduction :
Definition,
len( )
Creation, It returns length of the dictionary i.e. number
Accessing elements, of elements. Each element is a key:value pair.
Add an item,
Modify an item,
Traversal,
Membership
operator
Functions/
Methods:
len(), dict(),
keys(), Ex. One of the
key:value
values(), items(), pair
get(),
update(), del(), del,
clear(), fromkeys(), copy(),
pop(), popitem(),
setdefault(), max(), min(),
FUNCTIONS IN DICTIONARY: dict()
dict( ) dict() method
Creating empty dictionary with

It is a built-in function that returns a dictionary object


or simply creates a dictionary in Python.

Creating Dictionary from Key and Value Pairs in the form of sequences
Creating Dictionary using aTUPLE OF LISTS Creating Dictionary using a LIST OF LISTS containing
containing [key,value] pair using dict() method [key,value] pair using dict() method

Creating Dictionary using a TUPLE OF TUPLES


Creating Dictionary using a LIST OF TUPLES
containing (key,value) pair using dict()
containing (key,value) pair using dict() method
method

Creating Dictionary by zipping two Lists /or two Tuples containing Key and Values using zip( ) method and
sending the zipped sequences of key and values to the dict() method as parameter.
FUNCTIONS IN DICTIONARY: keys()
CONTENTS keys( )
Introduction : Returns a list containing the dictionary's keys
Definition,
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership
operator
Functions/
Methods:

len(), dict(),

keys(),
values(), items(),
get(), update(), LIST of all the keys of
EXTRA
del(), FACTS
del, clear(), the key:value pairs
fromkeys(),
If a Dictionary is created with similar keys, then the
copy(), pop(),
key is considered only once and its Value is from the
last occuring value of the key:value pair. Here, the key
popitem(),
10 has valuesmax(),
setdefault(), ‘x’ andmin(),
then ‘z’. So, the last occuring
value ‘z’;is taken as its value.
sorted()
FUNCTIONS IN DICTIONARY: values()
CONTENTS values( )
Introduction : Returns a list of all the values in the dictionary
Definition,
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods:
len(), dict(), keys(),
values(), items(),
get(), update(), del(), del,
clear(), fromkeys(),
copy(), pop(), popitem(),
setdefault(), max(), min(),
sorted() ;
Suggested programs : LIST of all the values of the key:value pair
FUNCTIONS IN DICTIONARY: items()
CONTENTS
Introduction :
items( )
Definition, Returns a list containing a tuple for each key
Creation, value pair.
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods:
len(), dict(), keys(),
values(),items() ,
get(), update(), del(),
del, clear(),
fromkeys(),
copy(), pop(),
popitem(), setdefault(),
max(), min(), sorted() ;
Suggested programs
FUNCTIONS IN DICTIONARY: get()
CONTENTS
Introduction :
get( )
Definition, Returns the value of the specified key
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods:
len(), dict(), keys(),
values(), items(),

get(), update(),
del(), del, clear(),
fromkeys(), copy(),
pop(), popitem(), Elements can be accessed using get() method with
setdefault(), max(),
min(), sorted() ; Dictionary or directly by writing the key inside [ ]
Suggested with the Dictionary name
FUNCTIONS IN DICTIONARY: update()
CONTENTS update( )
Introduction : Updates the dictionary with the specified key-value pairs.
Definition,
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods:
len(), dict(), keys(),
values(), items(), get(),

update(),
del(), del, clear(),
fromkeys(), copy(),
pop(), popitem(),
setdefault(), max(),
min(), sorted() ;
Suggested programs
FUNCTIONS IN DICTIONARY: del()
CONTENTS
Introduction :
del( ) method
Definition, It removes an entire dictionary
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods
len(), dict(), keys(),
values(), items(), get(),

update(), del(),
del,
clear(), fromkeys(),
copy(), pop(),
popitem(), setdefault(),
max(), min(),sorted();
KEYWORD IN DICTIONARY: del
CONTENTS del
• It removes a key-value pair from the dictionary based on the key.
Introduction :
keyword •If the key is present in the dictionary then it will delete the key and
Definition, corresponding value from the dictionary.
Creation, •But if the given key is not present in the dictionary then it will
Accessing elements, throw an error i.e. KeyError.
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods
len(), dict(), keys(),
values(), items(), get(),

update(), del(), del ,


clear(), fromkeys(),
copy(), pop(),
popitem(), setdefault(),
max(), min(),sorted() ;
Suggested programs
FUNCTIONS IN DICTIONARY: clear()
CONTENTS
Introduction :
Definition, clear( )
Creation, Removes all the elements from the dictionary.
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods –
len(), dict(), keys(), values(),
items(), get(), update(), del(),

del,clear() ,
fromkeys(), copy(), pop(),
popitem(), setdefault(),
max(), min(), sorted() ;
Suggested programs :
FUNCTIONS IN DICTIONARY: fromkeys()
CONTENTS fromkeys( )
Introduction : Returns a dictionary with the specified keys and value.
Definition, Syntax
Creation, dict.fromkeys(keys, value)
Accessing elements,
Add an item,
Parameter Values
Modify an item, Parameter Description
Traversal, keys Required. An iterable specifying the keys of the new dictionary
Membership operator value Optional. The value for all keys. Default value is None
Functions/Methods
len(), dict(), keys(),
values(), items(), get(),
update(), del(), del,
clear(),
fromkeys(),
copy(), pop(), popitem(),
setdefault(), max(),
min(), sorted();
Suggested programs
FUNCTIONS IN DICTIONARY: copy()
CONTENTS copy( )
Introduction : Returns a shallow copy of the dictionary. (Shallow copy of the
Definition, dictionary gets created where only keys are duplicated and
Creation, same values are referenced by the two).
Accessing elements, If values are immutable, changes are NOT reflected in the
Add an item, copy.
Modify an item, If values are mutable, changes are reflected in the copy.
Traversal,
Membership operator
A new dictionary(copy) is created with the elements
Functions/Methods (key:value) pairs of the original (old) dictionary.
len(), dict(), keys(), No change in the original dictionary. Original dictionary
values(), items(), get(), remains the same.
update(), del(), del, SYNTAX
clear(), fromkeys(), newdict=originaldict.copy()

There are two ways of copying a dictionary.


copy(), pop(),
popitem(), setdefault(), 1. Using assignment operator (=)
max(), min(), sorted()
Suggested programs 2. Using copy() method
FUNCTIONS IN DICTIONARY: copy() contd.
1. Using assignment operator (=)
It will create the copy where the two labels will be referencing the same
dictionary. Either changes in one dictionary will get reflected in the other.

Here, values of
Dictionary D are
assigned to a new label
T (Dictionary)

Here, Changes are


done in Dictionary D
only.

Here, Changes get


reflected in both the
Dictionaries D and T.
FUNCTIONS IN DICTIONARY: copy() contd.
2. Using copy() method
a copy of keys is created with the new name and the values referenced are shared by the
two copies. Again, there can be two scenarios on the basis of the VALUES being
Immutable or Mutable.
Scenario (A): when VALUES are IMMUTABLE (such Scenario (B):
as Strings or numbers) when VALUES are MUTABLE (such as Lists)
Any changes in the copy created with copy() will not keys will be referencing to the same Python list
be reflected in the original dictionary as the copied objects (memory addresses of the values won’t
set has own set of referencing keys. change) but lists being mutable can change

Here, in both the


Scenarios, original
Dictionary D is
copied to a new label
T (Dictionary)
Here, in both
scenarios, if there
is any change done
in any of the
Dictionaries, it
DOES NOT get
reflected in the
other Dictionary.
FUNCTIONS IN DICTIONARY: pop()
CONTENTS pop( )
Introduction : Removes the element with the specified key
Definition, SYNTAX
Creation, dict.pop(key)
Accessing elements, Where key is the key of the key:value pair which is being
Add an item, deleted.
Modify an item,
Traversal,
Membership operator
Functions/Methods –
len(), dict(), keys(), pop() deletes the specified key:value
values(), items(), get(), pair whose key if given as argument.
update(), del(), del,
clear(), fromkeys(),

copy(), pop() ,
popitem(), setdefault(),
max(), min(), sorted() ; Here, age is the key and 12 is the
Suggested programs : value which is being removed.
FUNCTIONS IN DICTIONARY: popitem()
CONTENTS
popitem( )
Introduction : Removes the last inserted key:value pair.
Definition, Also, returns the key:value pair being deleted in the
Creation, form of a tuple with the key and its value.
Accessing elements, SYNTAX
Add an item, Dict.popitem()
Modify an item,
Traversal,
Membership operator
Functions/Methods –
len(), dict(), keys(), popitem() deletes the last key:value
pair occurring in the Dictionary. Also,
values(), items(), get(), returns the key,value pair being deleted
update(), del(), del, in a tuple form.
clear(), fromkeys(), copy(),
pop(), popitem() ,
setdefault(), max(), min(),
sorted() ;
Suggested programs :
FUNCTIONS IN DICTIONARY: setdefault()
CONTENTS setdefault( ) Returns the value of the specified key. If the key
does
Introduction :
not exist: insert the key, with the specified value.
Definition,
Syntax
Creation,
dictionary.setdefault(keyname, value)
Accessing elements, keyname Required. The keyname of the item you want to return the value from
Add an item, value Optional.
Modify an item, If the key exist, this parameter has no effect.
If the key does not exist, this value becomes the key's value
Traversal, Default value None
Membership operator
Functions/Methods
len(), dict(), keys(),
values(), items(), get(),
update(), del(), del, Here, key 50 doesn’t exist, so, key
clear(), fromkeys(), 50 and its value ‘s’ has been taken.
copy(), pop(),
popitem(),
setdefault(), Here, key 20 exists, so, key 20 and
its old value ‘n’ has been taken.
max(), min(), sorted() ;
Suggested
Here, new key 70 doesn’t exist, and no value
programs is provided. So, value None has been taken.
FUNCTIONS IN DICTIONARY: max()
CONTENTS
max( )
Introduction : To find key with Maximum value in Dictionary
Definition,
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods Here, Month is the name of the dictionary. The keys are numeric.
len(), dict(), keys(),
values(), items(), get(),
update(), del(), del,
clear(), fromkeys(),
copy(), pop(),
popitem(), setdefault(),
max(), min(), Here, D is the dictionary. The keys are characters (alphabets).
sorted() ;
So, they are checked as per the ASCII rule.
Suggested programs In ASCII, A=65,B=66….so on a=97,b=98,……….so on
FUNCTIONS IN DICTIONARY: min()
CONTENTS min( )
Introduction : To find key with Minimum value in Dictionary
Definition,
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator Here, Month is the name of the dictionary. The keys are numeric.
Functions/Methods –
len(), dict(), keys(),
values(), items(), get(),
update(), del(), del,
clear(),
fromkeys(), copy(), pop(),
popitem(), setdefault(),
max(), min(),sorted() ; Here, D is the dictionary. The keys are characters (alphabets).
Suggested programs : So, they are checked as per the ASCII rule.
In ASCII, A=65,B=66….so on a=97,b=98,……….so on
FUNCTIONS IN DICTIONARY: sorted()
CONTENTS sorted( )
It returns the list of keys sorted in ascending order by default.
Introduction :
For the list of keys in descending order, reverse=True
Definition,
Creation,
Accessing elements,
Add an item,
Modify an item,
Traversal,
Membership operator
Functions/Methods –
len(), dict(), keys(),
values(), items(), get(),
update(), del(), del,
clear(), fromkeys(),
copy(), pop(),
popitem(),
setdefault(), max(),

min(), sorted() ;
Suggested programs :
NESTED
DICTIONAR
Y
NESTED DICTIONARIES
 Dictionary within a Dictionary is called as Nested Dictionary.
 The outer Dictionary is known as ENCLOSING Dictionary.
 The inner Dictionary is known as the NESTED dictionary.
 We can give a DICTIONARY as a VALUE to another
Dictionary.
 We CANNOT give a DICTIONARY as a KEY to
another
Dictionary as Dictionaries are mutable..

OUTPUT
SOME
PROGRAMS
IN
DICTIONAR
PROGRAM 1:
Count the number of times a character/digit appears
in a given string using a dictionary

OUTPUT
PROGRAM 2:
Create a dictionary with employee numbers, names of employees,
their salary and display them.

OUTPUT

You might also like