DICTIONARY FUNCTIONS AND METHODS
-RANJANA.V
GETTING LENGTH OF A DICTIONARY
THE LEN( ) FUNCTION
Syntax - len(<dictionary>)
 For example –
 tit={'name':'tom', 'sal':'123',
'age':'14'}
>>>len(tit)
3
ACCESSING ITEMS, KEYS AND VALUES
 Syntax- <dictionary>.get( key )
 To get the value of a key
For example:
tit={'name':'tom', 'sal':'123', 'age':'14'}
>>>tit.get(‘age’)
‘14’
 It returns all the items in the dictionary as a
sequence of key,value pair
 It returns them as tuples
For example:
tit={'name':'tom', 'sal':'123', 'age':'14'}
>>>tit.items()
([ ('name':'tom‘), ('sal':'123‘),( 'age':'14‘)
])
The item( ) method
The get( ) method
 It returns all the keys in a dictionary
 It returns them in list form
 Syntax- <dictionary>.keys( )
 For example:
 tit={'name':'tom', 'sal':'123',
'age':'14'}
>>>tit.keys( )
['name‘, 'sal’, 'age']
 It returns all the values of a
dictionary
 It returns in list form
 Syntax - <dictionary>. values()
 For example:
 tit={'name':'tom', 'sal':'123', 'age':'14'}
>>>tit.values( )
[‘tom’, ‘123’, ‘14’]
The keys() method The values( ) method
CREATING DICTIONARIES FROM KEYS
 It contains sequence of keys of the new
dictionary
 The key can be in form of list or tuple
 It has one common value
 If no value given , value None ,is
assigned to value
 SYNTAX-
 dict.fromkeys(<keys sequence>,
The fromkeys( ) method
 nd=dict.fromkeys([2,3,4,5,], 100)
>>>nd
{2:100, 3:100 , 4:100, 5:100}
nd=dict.fromkeys((2,3,4,5,))………..ex-2
>>>nd
{2:None,3:None,4:None,5:None}
THE UPDATE( ) METHODS
 This method merges key: value pairs from the new dictionary into the original dictionary
 Existing keys will be updated with new values
 Syntax-
 <dictionary>.update(<new dictionary>)
 For example:
 get={‘a’:5,’b’:2,’c’:3, r:1}
 Tit={‘a’:11’,sam’:12,’c’:’14’}
>>>get.update(tit)
>>>get
{‘a’:11,’sam’:12,’b’:2, ‘c’:14,’r’:1}
SHALLOW COPY
 Syntax-<dict>.copy
 For example:
 tit={'name':'tom', 'sal':'123', 'age':'14'}
 tit2=tit.copy( )
 print(tit2)
 tit={'name':'tom', 'sal':'123', 'age':'14'}
 tit2[ham]=‘food’ ….
 tit2={'name':'tom', 'sal':'123', 'age':'14‘,’ham’:’food’}
 tit={'name':'tom', 'sal':'123', 'age':'14'}
Updating
GET SORTED LIST OF KEYS – THE SORTED( ) FUNCTION
Here keys are sorted sorted(<dict>) for ascending order
It returns in form of list sorted(<dict>,[reverse=True])
 tit={5:’jas’,22:’sam’,13:’tom’,4:’tim’}
 tit2=sorted(tit)
 print(tit2)
 [4,5,14,22]
ascending order
 tit={5:’jas’,22:’sam’,13:’tom’,4:’tim’}
 tit2=sorted(tit2,[reverse=true])
 print(tit2)
 [22,13,5,4]
 Descending order
CALCULATING MAXIMUM , MINIMUM , SUM- MAX() , MIN(), SUM(),
FUNCTIONS
 Tit={5:’jas’,22:’sam’,13:’tom’,4:’tim’} Tit={5:’jas’,22:’sam’,13:’tom’,4:’tim’}
 Print (max(tit)) Print (min(tit))
 22 4
 These function works on keys
max min
SUM()
 sums() can only work with dictionary having keys which are addition compatible
 Example
 d2={‘green’:20,’blue’:30,yellow:40,’grey’:50}
 Print(sum(d2))
 d3={5:’jas’,22:’sam’,13:’tom’,4:’tim’}
 Print(sum(d3))
 44
error
THANK YOU
RANJANA12MALATH@GMAIL.COM

Dictionary functions and methods.ppt .

  • 1.
    DICTIONARY FUNCTIONS ANDMETHODS -RANJANA.V
  • 2.
    GETTING LENGTH OFA DICTIONARY THE LEN( ) FUNCTION Syntax - len(<dictionary>)  For example –  tit={'name':'tom', 'sal':'123', 'age':'14'} >>>len(tit) 3
  • 3.
    ACCESSING ITEMS, KEYSAND VALUES  Syntax- <dictionary>.get( key )  To get the value of a key For example: tit={'name':'tom', 'sal':'123', 'age':'14'} >>>tit.get(‘age’) ‘14’  It returns all the items in the dictionary as a sequence of key,value pair  It returns them as tuples For example: tit={'name':'tom', 'sal':'123', 'age':'14'} >>>tit.items() ([ ('name':'tom‘), ('sal':'123‘),( 'age':'14‘) ]) The item( ) method The get( ) method
  • 4.
     It returnsall the keys in a dictionary  It returns them in list form  Syntax- <dictionary>.keys( )  For example:  tit={'name':'tom', 'sal':'123', 'age':'14'} >>>tit.keys( ) ['name‘, 'sal’, 'age']  It returns all the values of a dictionary  It returns in list form  Syntax - <dictionary>. values()  For example:  tit={'name':'tom', 'sal':'123', 'age':'14'} >>>tit.values( ) [‘tom’, ‘123’, ‘14’] The keys() method The values( ) method
  • 5.
    CREATING DICTIONARIES FROMKEYS  It contains sequence of keys of the new dictionary  The key can be in form of list or tuple  It has one common value  If no value given , value None ,is assigned to value  SYNTAX-  dict.fromkeys(<keys sequence>, The fromkeys( ) method  nd=dict.fromkeys([2,3,4,5,], 100) >>>nd {2:100, 3:100 , 4:100, 5:100} nd=dict.fromkeys((2,3,4,5,))………..ex-2 >>>nd {2:None,3:None,4:None,5:None}
  • 6.
    THE UPDATE( )METHODS  This method merges key: value pairs from the new dictionary into the original dictionary  Existing keys will be updated with new values  Syntax-  <dictionary>.update(<new dictionary>)  For example:  get={‘a’:5,’b’:2,’c’:3, r:1}  Tit={‘a’:11’,sam’:12,’c’:’14’} >>>get.update(tit) >>>get {‘a’:11,’sam’:12,’b’:2, ‘c’:14,’r’:1}
  • 7.
    SHALLOW COPY  Syntax-<dict>.copy For example:  tit={'name':'tom', 'sal':'123', 'age':'14'}  tit2=tit.copy( )  print(tit2)  tit={'name':'tom', 'sal':'123', 'age':'14'}  tit2[ham]=‘food’ ….  tit2={'name':'tom', 'sal':'123', 'age':'14‘,’ham’:’food’}  tit={'name':'tom', 'sal':'123', 'age':'14'} Updating
  • 8.
    GET SORTED LISTOF KEYS – THE SORTED( ) FUNCTION Here keys are sorted sorted(<dict>) for ascending order It returns in form of list sorted(<dict>,[reverse=True])  tit={5:’jas’,22:’sam’,13:’tom’,4:’tim’}  tit2=sorted(tit)  print(tit2)  [4,5,14,22] ascending order  tit={5:’jas’,22:’sam’,13:’tom’,4:’tim’}  tit2=sorted(tit2,[reverse=true])  print(tit2)  [22,13,5,4]  Descending order
  • 9.
    CALCULATING MAXIMUM ,MINIMUM , SUM- MAX() , MIN(), SUM(), FUNCTIONS  Tit={5:’jas’,22:’sam’,13:’tom’,4:’tim’} Tit={5:’jas’,22:’sam’,13:’tom’,4:’tim’}  Print (max(tit)) Print (min(tit))  22 4  These function works on keys max min
  • 10.
    SUM()  sums() canonly work with dictionary having keys which are addition compatible  Example  d2={‘green’:20,’blue’:30,yellow:40,’grey’:50}  Print(sum(d2))  d3={5:’jas’,22:’sam’,13:’tom’,4:’tim’}  Print(sum(d3))  44 error
  • 11.