Dictionary
Dictionary
WHAT IS DICTIONARY?
WHAT IS
DICTIONARY?
• A dictionary is a collection data type
that is used to store data values in
key:value pairs. It is ordered,
changeable and do not allow
duplicates.
We can refer to a
dictionary as a mapping
between a set of indices
(which are called keys) and a
set of values. Each key maps a
value. The association of a key
and a value is called a key-
value pair.
Syntax:
my_dict = {'key1':
DICTIONARIES
A dictionary is an un
ordered collection of key-
value pairs.
>>>
A={1:"one",2:"two",3:"three
"} 1 one
A= 2 two
3 three
KEY VALUES
S
CREATING A DICTIONARY
– dict()
CREATING DICTIONARAY – dict()
OUT PUT
CREATING AND TRAVERSING
DICTIONARAY
OUT PUT
ACCESSING VALUES IN
DICTIONARY:
• To access dictionary elements, you use the
familiar square brackets along with the key to
obtain its value:
• Example:
dict = {'Name': 'Zara', 'Age': 7, 'Class':
'First'};
Print("dict['Name']: ", dict['Name’](;
print (“dict['Age']: ", dict['Age’]);
• This will produce following result:
dict['Name']: Zara
dict['Age']: 7
• If we attempt to access a data item with a key
which is not part of the dictionary, we get an error
as follows:
dict = {'Name': 'Zara', 'Age': 7, 'Class':
'First'};
print("dict['Alice']: ", dict['Alice’]);
• This will produce following result:
dict['Zara']:
Traceback (most recent call last): File "test.py",
line 4, in <module> print "dict['Alice']: ",
dict['Alice']; KeyError: 'Alice'
UPDATING DICTIONARY:
dict['Age']: 8
dict['School']: DPS School
DELETE DICTIONARY ELEMENTS:
• You can either remove individual dictionary elements or clear the entire contents
of a dictionary. You can also delete entire dictionary in a single operation.
• To explicitly remove an entire dictionary, just use the del statement:
• Example:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
del dict['Name']; # remove entry with key 'Name’
dict.clear(); # remove all entries in dict
del dict ; # delete entire dictionary
print "dict['Age']: ", dict['Age'];
print "dict['School']: ", dict['School'];
• This will produce following result. Note an exception raised, this is because after
del dict dictionary does not exist any more:
dict['Age']:
Traceback (most recent call last): File "test.py", line 8, in <module> print
"dict['Age']: ", dict['Age'];
TypeError: 'type' object is unsubscriptable
1 cmp(dict1, dict2)
2 len(dict)
Gives the total length of the dictionary. This would be equal to the number
of items in the dictionary.
3 str(dict)
4 type(variable)
Dictionary Meaning
Method
dict.pop() returns list of dictionary
dict's keys
dict.popitem() similar to get(), but will
set dict[key]=default if
key is not already in dict
dict.clear() METHOD
clear
method
OUTPUT is in next
dict.clear() METHOD -
OUTPUT
Birthday dictionary
dict.copy() METHOD
copy
method
OUTPUT is in next
dict.copy() METHOD -
OUTPUT
get
method
OUTPUT is in next
dict.get() METHOD -
OUTPUT
items
method
OUTPUT is in next
dict.items() METHOD -
OUTPUT
keys
method
OUTPUT is in next
dict.keys() METHOD -
OUTPUT
update
method
dict.values() METHOD
Sample Dictionary
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7:
49, 8: 64, 9: 81, 10: 100, 11: 121, 12:
144, 13: 169, 14: 196, 15: 225}