Python Programming – Dictionary

In this Page, We are Providing Python Programming – Dictionary. Students can visit for more Detail and Explanation of Python Handwritten Notes Pdf.

Python Programming – Dictionary

Dictionary

Another useful mutable built-in type is “dictionary”. A dictionary is an unordered group of comma-separated “key: value” pairs enclosed within braces, with the requirement that the keys are unique within a dictionary. The main operation of a dictionary is storing a value corresponding to a given key and extracting the value for that given key. Unlike sequences, which are indexed by a range of numbers, the dictionary is indexed by key (key should be of an immutable type, strings and numbers can always be keys).

Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. The list cannot be used as keys, since lists are of mutable type. Also, as mentioned previously, Python allows adding a trailing comma after the last item of the dictionary.

>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }
>>> a
{ ' sape ' : 4139 , ' jack ' : 4098 , ' guido ' : 4127 }
>>> a [ ' jack ' ]
4098
>>> a= { ' sape ' : 4139, ' guido ' : 4127, ' jack ' : 4098 , }
>>> a
{ ' sape ' : 4139 , ' jack ' : 4098 , ' guido ' : 4127 }

Dictionary creation

Dictionary can be created in many ways.

Using curly braces

Placing a comma-separated record of key-value pairs within the braces adds initial key-value pairs to the dictionary; this is also the way dictionaries are written as output.

>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }
>>> a 
{ ' sape ' , : 4139 , ' jack ' : 4098 , ' guido ' : 412 7 }

A pair of braces creates an empty dictionary.

>>> a= { }
>>> a 
{ }
>>> type ( a ) 
<type ' dict ' >

Dictionary comprehension

Dictionary comprehension provides a concise way to create a dictionary.

>>> { x : x**2 for x in ( 2 , 4 , 6 ) } 
{ 2 : 4 , 4 : 16 , 6 : 36 }

Using built-in function

Dictionary can also be created using a built-in function diet ( ). Consider the following example using diet (), which returns the same dictionary { ” one ” : 1 , ” two ” : 2 , ” three ” : 3 } :

>>> a=dict ( one=1 , two=2 , three=3 )
>>> b= { ' one ' : 1 , ' two ' : 2 , ' three ' : 3 }
>>> c=dict ( zip ( [ ' one ' , ' two ' , ' three ' ] , [ 1 , 2 , 3 ] ) ) 
>>> d=dict ( [ ( ' two', 2), ( ' one ', 1), ( ' three ', 3) J )
>>> e=dict ( { ' three ' : 3 , ' one ' : 1 , ' two ' : 2 } )
>>> a==b==c==d==e 
True

Accessing dictionary elements

To access a dictionary value, use the key enclosed within the square bracket. A KeyError exception is raised if the key is not present in the dictionary.

>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }
>>> a [ ' guido ' ]
4127

Updating dictionary elements

It is possible to add a new item in a dictionary and can also change the value for a given key.

>>> a= { 'sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }
>>> a [ ' mike ' ]=2299 # Add new item
>>> a [ ' guido ' ]=1000 # Update existing item
>>> a
{ ' sape ' : 4139 , ' mike ' : 2299 , ' jack ' : 4098 , ' guido ' : 1000 }

It is also possible to update a dictionary with another dictionary using the update ( ) method (discussed later).

Deleting dictionary elements

To remove a dictionary item (key-value pair) or the entire dictionary, one can use the del statement. To remove all items (resulting in an empty dictionary), the clear ( ) method (discussed later) can be used.

>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 } 
>>> del a [ ' guido ' ]
>>> a
{ ' sape ' : 4139 , ' jack ' : 4098 }
>>> del a
>>> a
Traceback ( most recent call last ) :
File " <stdin> ", line 1, in <module>
NameError: name ' a ' is not defined

Membership operation

Dictionary support membership operation i.e. checking the existence of a key in the dictionary.

>>> a= { 'sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }
>>> ' jack ' in a 
True
>>> ' tom ' not in a 
True
>>> 4127 in a 
False

Looping techniques

When looping through the dictionary, the key and corresponding value can be retrieved at the same time using the iteritems ( ) method.

>>> a= { ' sape ' : 4139 , ' guido ' : 4127 , ' jack ' : 4098 }
>>> for k , v in a . iteritems ( ) :
. . . print k , v
sape 4139 
jack 4098 
guido 4127