SlideShare a Scribd company logo
1 of 12
Python Dictionary
Add a Slide Title - 4
• A dictionary is mutable and is another container
type that can store any number of Python objects
• Dictionaries consist of pairs (called items) of keys
and their corresponding values.
• Python dictionaries are also known as associative
arrays or hash tables.
• The general syntax of a dictionary is as follows:
Dog = {‘name': ‘Marley', ‘color': ‘Gold', ‘legs': ‘4'}
or
Dog=dict(name=“Marley", color=“Gold”,legs=4);
Python Dictionary:
Python Dictionary Features:
• Each key is separated from its value by a colon (:).
• The items are separated by commas, and the whole thing is enclosed
in curly braces.
• An empty dictionary without any items is written with just two curly
braces, like this: {}.
• Keys are unique within a dictionary while values may not be
Accessing in Dictionary:
• d.items()
• d.values()
• d.keys()
Accessing Values in Dictionary:
• To access dictionary elements, you can use the familiar square
brackets along with the key to obtain its value.
• For example:
student = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
print (“student['Name']: ", student['Name'])
● Output: student['Name']: Zara
Updating Dictionary:
• You can update a dictionary by adding a new entry or item (i.e., a key-value
pair)
• modifying an existing entry, or deleting an existing entry as shown below in
the simple example
• student = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
student['Age'] = 8; # update existing entry
student['School'] = "DPS School"; # Add new entry
print (“student['Age']: ", student['Age'])
print (“student['School']: ", student['School'])
• Output: student['Age']: 8 student['School']: DPS School
Another Formula:
Delete Dictionary Elements:
• You can either remove individual dictionary elements or clear the entire
contents of a dictionary
• To explicitly remove an entire dictionary, just use the del statement. i.e:
student = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};
• del student['Name']; # remove entry with key 'Name'
• student.clear(); # remove all entries in student del student ; # delete
entire dictionary
• print (“student['Age']: ", student['Age'])
• print (“student['School']: ", student['School'])
Properties of Dictionary Keys:
There are two important points to remember about dictionary keys:
(a) More than one entry per key not allowed. Which means no duplicate key is allowed.
When duplicate keys encountered during assignment, the last assignment wins.
• student= {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'};
• print (“student['Name']: ", student['Name']) //Output: student['Name']: Manni
(b) Keys must be immutable. Which means you can use strings or numbers as
dictionary keys but something like ['key'] is not allowed. Following is a simple example:
• student = {['Name']: 'Zara', 'Age': 7};
• print (student['Name']: ", student['Name']) //TypeError
Let’s Practice .. !
Problem I
• Write a Python program to check if a given
key already exists in a dictionary.
Problem II
. Write a Python program to remove duplicates
from Dictionary
Python dictionary
Python dictionary

More Related Content

What's hot (20)

Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
 
List in Python
List in PythonList in Python
List in Python
 
C Structures and Unions
C Structures and UnionsC Structures and Unions
C Structures and Unions
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Python Dictionary.pptx
Python Dictionary.pptxPython Dictionary.pptx
Python Dictionary.pptx
 
File operations in c
File operations in cFile operations in c
File operations in c
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
 
Python Dictionary
Python DictionaryPython Dictionary
Python Dictionary
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
Python Functions
Python   FunctionsPython   Functions
Python Functions
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Regular expressions in Python
Regular expressions in PythonRegular expressions in Python
Regular expressions in Python
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Python set
Python setPython set
Python set
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 

Similar to Python dictionary

Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1Vikram Nandini
 
An Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in PythonAn Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in Pythonyashar Aliabasi
 
Ch 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptxCh 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptxKanchanaRSVVV
 
Tuples, Dicts and Exception Handling
Tuples, Dicts and Exception HandlingTuples, Dicts and Exception Handling
Tuples, Dicts and Exception HandlingPranavSB
 
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csUNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csjaved75
 
DICTIONARIES (1).pptx
DICTIONARIES (1).pptxDICTIONARIES (1).pptx
DICTIONARIES (1).pptxKalashJain27
 
demo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.pptdemo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.pptFerdieBalang
 
PE1 Module 4.ppt
PE1 Module 4.pptPE1 Module 4.ppt
PE1 Module 4.pptbalewayalew
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developersJim Roepcke
 
Querying XML: XPath and XQuery
Querying XML: XPath and XQueryQuerying XML: XPath and XQuery
Querying XML: XPath and XQueryKatrien Verbert
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like PythonistaChiyoung Song
 

Similar to Python dictionary (20)

Dictionary.pptx
Dictionary.pptxDictionary.pptx
Dictionary.pptx
 
Ch_13_Dictionary.pptx
Ch_13_Dictionary.pptxCh_13_Dictionary.pptx
Ch_13_Dictionary.pptx
 
Python dictionary
Python dictionaryPython dictionary
Python dictionary
 
Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1Programming in python Unit-1 Part-1
Programming in python Unit-1 Part-1
 
An Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in PythonAn Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in Python
 
Ch 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptxCh 7 Dictionaries 1.pptx
Ch 7 Dictionaries 1.pptx
 
.net F# mutable dictionay
.net F# mutable dictionay.net F# mutable dictionay
.net F# mutable dictionay
 
1_5_Python_to_Java.pptx
1_5_Python_to_Java.pptx1_5_Python_to_Java.pptx
1_5_Python_to_Java.pptx
 
Tuples, Dicts and Exception Handling
Tuples, Dicts and Exception HandlingTuples, Dicts and Exception Handling
Tuples, Dicts and Exception Handling
 
bhaskars.pptx
bhaskars.pptxbhaskars.pptx
bhaskars.pptx
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csUNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year cs
 
DICTIONARIES (1).pptx
DICTIONARIES (1).pptxDICTIONARIES (1).pptx
DICTIONARIES (1).pptx
 
bhaskars.pptx
bhaskars.pptxbhaskars.pptx
bhaskars.pptx
 
demo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.pptdemo1 java of demo 1 java with demo 1 java.ppt
demo1 java of demo 1 java with demo 1 java.ppt
 
PE1 Module 4.ppt
PE1 Module 4.pptPE1 Module 4.ppt
PE1 Module 4.ppt
 
Unit 1(Lesson7).pptx
Unit 1(Lesson7).pptxUnit 1(Lesson7).pptx
Unit 1(Lesson7).pptx
 
Introduction to Python for Plone developers
Introduction to Python for Plone developersIntroduction to Python for Plone developers
Introduction to Python for Plone developers
 
Querying XML: XPath and XQuery
Querying XML: XPath and XQueryQuerying XML: XPath and XQuery
Querying XML: XPath and XQuery
 
Code Like Pythonista
Code Like PythonistaCode Like Pythonista
Code Like Pythonista
 

Recently uploaded

Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 

Recently uploaded (20)

Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 

Python dictionary

  • 2. Add a Slide Title - 4 • A dictionary is mutable and is another container type that can store any number of Python objects • Dictionaries consist of pairs (called items) of keys and their corresponding values. • Python dictionaries are also known as associative arrays or hash tables. • The general syntax of a dictionary is as follows: Dog = {‘name': ‘Marley', ‘color': ‘Gold', ‘legs': ‘4'} or Dog=dict(name=“Marley", color=“Gold”,legs=4); Python Dictionary:
  • 3. Python Dictionary Features: • Each key is separated from its value by a colon (:). • The items are separated by commas, and the whole thing is enclosed in curly braces. • An empty dictionary without any items is written with just two curly braces, like this: {}. • Keys are unique within a dictionary while values may not be
  • 4. Accessing in Dictionary: • d.items() • d.values() • d.keys()
  • 5. Accessing Values in Dictionary: • To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value. • For example: student = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}; print (“student['Name']: ", student['Name']) ● Output: student['Name']: Zara
  • 6. Updating Dictionary: • You can update a dictionary by adding a new entry or item (i.e., a key-value pair) • modifying an existing entry, or deleting an existing entry as shown below in the simple example • student = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}; student['Age'] = 8; # update existing entry student['School'] = "DPS School"; # Add new entry print (“student['Age']: ", student['Age']) print (“student['School']: ", student['School']) • Output: student['Age']: 8 student['School']: DPS School
  • 8. Delete Dictionary Elements: • You can either remove individual dictionary elements or clear the entire contents of a dictionary • To explicitly remove an entire dictionary, just use the del statement. i.e: student = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}; • del student['Name']; # remove entry with key 'Name' • student.clear(); # remove all entries in student del student ; # delete entire dictionary • print (“student['Age']: ", student['Age']) • print (“student['School']: ", student['School'])
  • 9. Properties of Dictionary Keys: There are two important points to remember about dictionary keys: (a) More than one entry per key not allowed. Which means no duplicate key is allowed. When duplicate keys encountered during assignment, the last assignment wins. • student= {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'}; • print (“student['Name']: ", student['Name']) //Output: student['Name']: Manni (b) Keys must be immutable. Which means you can use strings or numbers as dictionary keys but something like ['key'] is not allowed. Following is a simple example: • student = {['Name']: 'Zara', 'Age': 7}; • print (student['Name']: ", student['Name']) //TypeError
  • 10. Let’s Practice .. ! Problem I • Write a Python program to check if a given key already exists in a dictionary. Problem II . Write a Python program to remove duplicates from Dictionary