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

PPSX
python Function
Ronak Rathi
ย 
PPT
Python Dictionaries and Sets
Nicole Ryan
ย 
PDF
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
ย 
PPTX
Datastructures in python
hydpy
ย 
PPTX
Basic data structures in python
Celine George
ย 
PDF
Python set
Mohammed Sikander
ย 
PPTX
Python array
Arnab Chakraborty
ย 
PPT
structure and union
student
ย 
PPTX
Stack and Queue
Apurbo Datta
ย 
PPTX
C string
University of Potsdam
ย 
PPTX
List in Python
Siddique Ibrahim
ย 
PDF
Python strings
Mohammed Sikander
ย 
PPTX
Functions in C
Kamal Acharya
ย 
PPTX
Inline function
Tech_MX
ย 
PDF
Python tuple
Mohammed Sikander
ย 
PPTX
Python: Modules and Packages
Damian T. Gordon
ย 
PPTX
Array in c++
Mahesha Mano
ย 
PDF
Python programming : Files
Emertxe Information Technologies Pvt Ltd
ย 
PPTX
Chapter 14 strings
Praveen M Jigajinni
ย 
python Function
Ronak Rathi
ย 
Python Dictionaries and Sets
Nicole Ryan
ย 
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
ย 
Datastructures in python
hydpy
ย 
Basic data structures in python
Celine George
ย 
Python set
Mohammed Sikander
ย 
Python array
Arnab Chakraborty
ย 
structure and union
student
ย 
Stack and Queue
Apurbo Datta
ย 
List in Python
Siddique Ibrahim
ย 
Python strings
Mohammed Sikander
ย 
Functions in C
Kamal Acharya
ย 
Inline function
Tech_MX
ย 
Python tuple
Mohammed Sikander
ย 
Python: Modules and Packages
Damian T. Gordon
ย 
Array in c++
Mahesha Mano
ย 
Python programming : Files
Emertxe Information Technologies Pvt Ltd
ย 
Chapter 14 strings
Praveen M Jigajinni
ย 

Similar to Python dictionary (20)

PPTX
Dictionary.pptx
RishuVerma34
ย 
PPTX
Ch_13_Dictionary.pptx
HarishParthasarathy4
ย 
PPTX
Dictionariesasdfghjsdfghjklsdfghjkl.pptx
cjrfailure
ย 
PDF
Programming in python Unit-1 Part-1
Vikram Nandini
ย 
PPTX
An Introduction to Tuple List Dictionary in Python
yashar Aliabasi
ย 
PPTX
6Dictionaries and sets in pythonsss.pptx
OkelloBenjamin2
ย 
PPTX
Meaning of Dictionary in python language
PRASHANTMISHRA16761
ย 
PPTX
Ch 7 Dictionaries 1.pptx
KanchanaRSVVV
ย 
PPTX
.net F# mutable dictionay
DrRajeshreeKhande
ย 
PPTX
1_5_Python_to_Java.pptx
ShivamChaturvedi67
ย 
PPTX
Tuples, Dicts and Exception Handling
PranavSB
ย 
PPTX
bhaskars.pptx
NaveenShankar34
ย 
PPTX
Python Tutorial Part 1
Haitham El-Ghareeb
ย 
PPT
UNIT-IV WT web technology for 1st year cs
javed75
ย 
PPTX
DICTIONARIES (1).pptx
KalashJain27
ย 
PPTX
PYTHON Data structures Fundamentals: DICTIONARIES
KanadamKarteekaPavan1
ย 
PPTX
Python Fundamental Data structures: Dictionaries
KanadamKarteekaPavan1
ย 
PPTX
bhaskars.pptx
Naveen316549
ย 
PPT
demo1 java of demo 1 java with demo 1 java.ppt
FerdieBalang
ย 
Dictionary.pptx
RishuVerma34
ย 
Ch_13_Dictionary.pptx
HarishParthasarathy4
ย 
Dictionariesasdfghjsdfghjklsdfghjkl.pptx
cjrfailure
ย 
Programming in python Unit-1 Part-1
Vikram Nandini
ย 
An Introduction to Tuple List Dictionary in Python
yashar Aliabasi
ย 
6Dictionaries and sets in pythonsss.pptx
OkelloBenjamin2
ย 
Meaning of Dictionary in python language
PRASHANTMISHRA16761
ย 
Ch 7 Dictionaries 1.pptx
KanchanaRSVVV
ย 
.net F# mutable dictionay
DrRajeshreeKhande
ย 
1_5_Python_to_Java.pptx
ShivamChaturvedi67
ย 
Tuples, Dicts and Exception Handling
PranavSB
ย 
bhaskars.pptx
NaveenShankar34
ย 
Python Tutorial Part 1
Haitham El-Ghareeb
ย 
UNIT-IV WT web technology for 1st year cs
javed75
ย 
DICTIONARIES (1).pptx
KalashJain27
ย 
PYTHON Data structures Fundamentals: DICTIONARIES
KanadamKarteekaPavan1
ย 
Python Fundamental Data structures: Dictionaries
KanadamKarteekaPavan1
ย 
bhaskars.pptx
Naveen316549
ย 
demo1 java of demo 1 java with demo 1 java.ppt
FerdieBalang
ย 
Ad

Recently uploaded (20)

PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
ย 
PDF
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
ย 
PDF
Salesforce Implementation Services Provider.pdf
VALiNTRY360
ย 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
ย 
PDF
AI Image Enhancer: Revolutionizing Visual Qualityโ€
docmasoom
ย 
PDF
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
ย 
PDF
Infrastructure planning and resilience - Keith Hastings.pptx.pdf
Safe Software
ย 
PPTX
TexSender Pro 8.9.1 Crack Full Version Download
cracked shares
ย 
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
ย 
PPTX
Cutting Optimization Pro 5.18.2 Crack With Free Download
cracked shares
ย 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
ย 
PPT
Brief History of Python by Learning Python in three hours
adanechb21
ย 
PDF
Why Are More Businesses Choosing Partners Over Freelancers for Salesforce.pdf
Cymetrix Software
ย 
PPTX
Presentation about Database and Database Administrator
abhishekchauhan86963
ย 
PPTX
Farrell__10e_ch04_PowerPoint.pptx Programming Logic and Design slides
bashnahara11
ย 
PDF
System Center 2025 vs. 2022; Whatโ€™s new, whatโ€™s next_PDF.pdf
Q-Advise
ย 
PPTX
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
ย 
PPTX
Chess King 25.0.0.2500 With Crack Full Free Download
cracked shares
ย 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
ย 
PDF
AWS_Agentic_AI_in_Indian_BFSI_A_Strategic_Blueprint_for_Customer.pdf
siddharthnetsavvies
ย 
Role Of Python In Programing Language.pptx
jaykoshti048
ย 
New Download FL Studio Crack Full Version [Latest 2025]
imang66g
ย 
Salesforce Implementation Services Provider.pdf
VALiNTRY360
ย 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
ย 
AI Image Enhancer: Revolutionizing Visual Qualityโ€
docmasoom
ย 
MiniTool Power Data Recovery Crack New Pre Activated Version Latest 2025
imang66g
ย 
Infrastructure planning and resilience - Keith Hastings.pptx.pdf
Safe Software
ย 
TexSender Pro 8.9.1 Crack Full Version Download
cracked shares
ย 
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
ย 
Cutting Optimization Pro 5.18.2 Crack With Free Download
cracked shares
ย 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
ย 
Brief History of Python by Learning Python in three hours
adanechb21
ย 
Why Are More Businesses Choosing Partners Over Freelancers for Salesforce.pdf
Cymetrix Software
ย 
Presentation about Database and Database Administrator
abhishekchauhan86963
ย 
Farrell__10e_ch04_PowerPoint.pptx Programming Logic and Design slides
bashnahara11
ย 
System Center 2025 vs. 2022; Whatโ€™s new, whatโ€™s next_PDF.pdf
Q-Advise
ย 
TRAVEL APIs | WHITE LABEL TRAVEL API | TOP TRAVEL APIs
philipnathen82
ย 
Chess King 25.0.0.2500 With Crack Full Free Download
cracked shares
ย 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
ย 
AWS_Agentic_AI_in_Indian_BFSI_A_Strategic_Blueprint_for_Customer.pdf
siddharthnetsavvies
ย 
Ad

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