Dict = { 1 : 'Suri' , 'Fname' : 'Sam' , 'Lname' : 'Saiksas' , 'village' : 'AKP' }
print ( Dict )
Built-in dict method to create a dictionary:
Dict2 = dict ( { 1 : 'john' , 2 : 'Pam' , 3 : 'google' } )
print ( Dict2 )
Using pairs to create a dictionary:
Dict3 = dict ( [ ('1' , 'Techies') , (2 , 'For') ] )
print ( Dict3 )
Create a dictionary out of other data structures - list of tuples
prints only one value if there are any duplicate tuples in the list:
list1 = [ (2 , 3) , (4 , 5) , (4 , 5) ]
print ( dict ( list1 ) )
Assign values to Dictionaries:
exDict = { }
exDict [ 'Key_1' ] = 'Welcome'
exDict [ 'Key_2' ] = 'Good'
exDict [ 'Key_3' ] = 'Morning'
exDict [ 'Key_4' ] = 'Deo'
print ( exDict )
Assign a value set to a key - Assigning multiple values to a single key:
exDict[ 'Key_1' ] = 'Welcome' , 'Mr' , 'Graham'
print ( exDict )
Fetching a value from a dictionary using Get Method:
print ( '\n' , exDict.get ( 'Key_4' ) )
print ( '\n' )
print ( "|{0}|,|{1}|,{2}|".format ( exDict [ 'Key_1' ] , exDict [ 'Key_2' ] , exDict [ 'Key_3' ] ) )
Deleting items from the dictionary:
print ( exDict.values ( ) )
exDict.pop ( 'Key_1' )
print ( "After popping an element " , exDict )
print ( '\n' , 'This is the copy of dict' , exDict.copy ( ) )
print ( '\n' )
print ( '\n' )
Delete an entire dict:
print ( exDict.values ( ) )
print ( '\n' )
print ( exDict.items ( ) )
exDict.clear ( )
print ( '\n' )
print ( exDict )