SlideShare a Scribd company logo
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

More Related Content

Similar to Dictionary functions and methods.ppt .

Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
MihirDatir
 
Day 1c access, select ordering copy.pptx
Day 1c   access, select   ordering copy.pptxDay 1c   access, select   ordering copy.pptx
Day 1c access, select ordering copy.pptx
Adrien Melquiond
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 36 of 184
The Ring programming language version 1.5.3 book - Part 36 of 184The Ring programming language version 1.5.3 book - Part 36 of 184
The Ring programming language version 1.5.3 book - Part 36 of 184
Mahmoud Samir Fayed
 
Python programming Sequence Datatypes -Tuples
Python programming Sequence Datatypes -TuplesPython programming Sequence Datatypes -Tuples
Python programming Sequence Datatypes -Tuples
BushraKm2
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
Mahmoud Samir Fayed
 
Python elements list you can study .pdf
Python elements list you can study  .pdfPython elements list you can study  .pdf
Python elements list you can study .pdf
AUNGHTET61
 
Python data structures
Python data structuresPython data structures
Python data structures
Tony Nguyen
 
Python data structures
Python data structuresPython data structures
Python data structures
Luis Goldster
 
Python data structures
Python data structuresPython data structures
Python data structures
James Wong
 
Python data structures
Python data structuresPython data structures
Python data structures
Young Alista
 
Python data structures
Python data structuresPython data structures
Python data structures
Fraboni Ec
 
Python data structures
Python data structuresPython data structures
Python data structures
Harry Potter
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
vikram mahendra
 
Beautiful python - PyLadies
Beautiful python - PyLadiesBeautiful python - PyLadies
Beautiful python - PyLadies
Alicia Pérez
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
MihirDatir1
 
Python data structures
Python data structuresPython data structures
Python data structures
kalyanibedekar
 
OOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptxOOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptx
Tanzila Kehkashan
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptx
Yagna15
 

Similar to Dictionary functions and methods.ppt . (20)

Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Day 1c access, select ordering copy.pptx
Day 1c   access, select   ordering copy.pptxDay 1c   access, select   ordering copy.pptx
Day 1c access, select ordering copy.pptx
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184
 
The Ring programming language version 1.5.3 book - Part 36 of 184
The Ring programming language version 1.5.3 book - Part 36 of 184The Ring programming language version 1.5.3 book - Part 36 of 184
The Ring programming language version 1.5.3 book - Part 36 of 184
 
Python programming Sequence Datatypes -Tuples
Python programming Sequence Datatypes -TuplesPython programming Sequence Datatypes -Tuples
Python programming Sequence Datatypes -Tuples
 
The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196The Ring programming language version 1.7 book - Part 26 of 196
The Ring programming language version 1.7 book - Part 26 of 196
 
Python elements list you can study .pdf
Python elements list you can study  .pdfPython elements list you can study  .pdf
Python elements list you can study .pdf
 
Python data structures
Python data structuresPython data structures
Python data structures
 
Python data structures
Python data structuresPython data structures
Python data structures
 
Python data structures
Python data structuresPython data structures
Python data structures
 
Python data structures
Python data structuresPython data structures
Python data structures
 
Python data structures
Python data structuresPython data structures
Python data structures
 
Python data structures
Python data structuresPython data structures
Python data structures
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
 
Beautiful python - PyLadies
Beautiful python - PyLadiesBeautiful python - PyLadies
Beautiful python - PyLadies
 
Pythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptxPythonlearn-08-Lists.pptx
Pythonlearn-08-Lists.pptx
 
Python data structures
Python data structuresPython data structures
Python data structures
 
OOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptxOOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptx
 
Lists.pptx
Lists.pptxLists.pptx
Lists.pptx
 

Recently uploaded

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 

Recently uploaded (20)

clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 

Dictionary functions and methods.ppt .

  • 1. DICTIONARY FUNCTIONS AND METHODS -RANJANA.V
  • 2. GETTING LENGTH OF A DICTIONARY THE LEN( ) FUNCTION Syntax - len(<dictionary>)  For example –  tit={'name':'tom', 'sal':'123', 'age':'14'} >>>len(tit) 3
  • 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
  • 4.  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
  • 5. 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}
  • 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 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
  • 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() 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