SlideShare a Scribd company logo
Dictionaries
Team Emertxe
Dictionaries
Introduction
Group of items arranged in the form of key-value pair
Example
d = {"Name": "Ram", "ID": 102, "Salary": 10000}
Program
#Print the entire dictionary
print(d)
#Print only the keys
print("Keys in dic: ", d.keys())
#Print only values
print("Values: ", d.values())
#Print both keys and value pairs as tuples
print(d.items())
Dictionaries
Operations
d = {"Name": "Ram", "ID": 102, "Salary": 10000}
1. To get the no. of pairs in the Dictionary n = len(d)
2. To modify the existing value d[salary] = 15000
3. To insert new key:value pair d["Dept"] = "Finance"
4. To delete the key:value pair del d["ID"]
5. To check whether the key is present in
dictionary
"Dept" in d
- Returns True, if it is present
6. We can use any datatype fro values, but keys should obey the rules
R1: Keys should be unique
Ex: emp = {10: "Ram", 20: "Ravi", 10: "Rahim"}
- Old value will be overwritten,
emp = {10: "Rahim", 20: "Ravi"}
R2: Keys should be immutable type. Use numbers, strings or tuples
If mutable keys are used, will get 'TypeError'
Dictionaries
Methods
clear() d.clear() Removes all key-value pairs from the d
copy() d1 = d.copy() Copies all items from ‘d’ into a new dictionary ‘d1’
fromkeys() d.fromkeyss(s, [,v])
Create a new dictionary with keys from sequence ‘s’ and
values all set to ‘v’
get() d.get(k, [,v])
Returns the value associated with key ‘k’.
If key is not found, it returns ‘v’
items() d.items()
Returns an object that contains key-value pairs of ‘d’.
The pairs are stored as tuples in the object
keys() d.keys() Returns a sequence of keys from the dictionary ‘d’
values() d.values() Returns a sequence of values from the dictionary ‘d’
update() d.update(x) Adds all elements from dictionary ‘x’ to ‘d’
pop() d.pop(k, [,v]) Removes the key ‘k’ and its value.
Dictionaries
Programs
To create the dictionary with employee details
d = {"Name": "Ram", "ID": 1023, "Salary": 10000}
#Print the entire dictionary
print(d)
#Print only the keys
print("Keys in dic: ", d.keys())
#Print only values
print("Values: ", d.values())
#Print both keys and value pairs as tuples
print(d.items())
Dictionaries
Programs
#To create a dictionary from the keyboard and display the items
x = {}
print("Enter 'n' value: ", end='')
n = int(input())
for i in range(n):
print("Enter the key: ", end='')
k = input()
print("Enter the value: ", end='')
v = int(input())
x.update({k: v})
print(x)
Dictionaries
Using for loop with Dictionaries
Method-1
for k in colors:
print(k)
Method-2
for k in colors:
print(colors[k])
Method-3
for k, v in colors.items():
print("key = {}nValue = {}". format(k, v))
Dictionaries
Sorting Dictionaries: Exercise
To sort the elements of a dictionary based on akey or value
Dictionaries
Converting Lists into Dictionary
Two step procedure
- zip()
- dict()
#To convert list into dictionary
countries = ["India", "USA"]
cities = ["New Delhi", "Washington"]
#Make a dictionary
z = zip(countries, cities)
d = dict(z)
print(d)
Dictionaries
Converting strings into dictionary
str = "Ram=23,Ganesh=20"
#Create the empty list
lst = []
for x in str.split(','):
y = x.split('=')
lst.append(y)
#Convert into dictionary
d = dict(lst)
print(d)
Dictionaries
Passing dictionary to function
By specifying the name of the dictionary as the parameter, we can pass the dictionary to the
function.
Example
d = {10: "Ram"}
display(d)
Dictionaries
Ordered Dictionaries
from collections import OrderedDict
Example
d = {10: "Ram"}
display(d)
Program:
#To create the ordered dictionary
from collections import OrderedDict
#Create empty dictionary
d = OrderedDict()
d[10] = 'A'
d[11] = 'B'
d[12] = 'C'
d[13] = 'D'
print(d)
THANK YOU

More Related Content

PPTX
Loops c++
PPTX
Variables in python
PDF
Datatypes in python
PPTX
Looping Statements and Control Statements in Python
PPTX
Unit 4 python -list methods
PDF
Python Programming - XI. String Manipulation and Regular Expressions
PPTX
Tuple in python
PDF
Namespaces
Loops c++
Variables in python
Datatypes in python
Looping Statements and Control Statements in Python
Unit 4 python -list methods
Python Programming - XI. String Manipulation and Regular Expressions
Tuple in python
Namespaces

What's hot (20)

PPTX
Array in c++
PPTX
Linked list
PDF
Introduction to c++ ppt
PDF
Functions and modules in python
PDF
Python programming : Standard Input and Output
PDF
Python Unit 3 - Control Flow and Functions
PDF
Python Variable Types, List, Tuple, Dictionary
PPTX
Constants, Variables, and Data Types
PPTX
Dictionary in python
PDF
Introduction to c++ ppt 1
PPTX
Regular expressions in Python
PPTX
Introduction to python
PPSX
Modules and packages in python
PDF
Control Structures in Python
PPSX
Data Types & Variables in JAVA
PPT
Constants in C Programming
PPTX
Python Functions
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PDF
Python programming : Control statements
Array in c++
Linked list
Introduction to c++ ppt
Functions and modules in python
Python programming : Standard Input and Output
Python Unit 3 - Control Flow and Functions
Python Variable Types, List, Tuple, Dictionary
Constants, Variables, and Data Types
Dictionary in python
Introduction to c++ ppt 1
Regular expressions in Python
Introduction to python
Modules and packages in python
Control Structures in Python
Data Types & Variables in JAVA
Constants in C Programming
Python Functions
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Python programming : Control statements
Ad

Similar to Python : Dictionaries (20)

PPTX
python advanced data structure dictionary with examples python advanced data ...
PPTX
Dictionaries.pptx
PDF
Python dictionaries
PPTX
Dictionary in python Dictionary in python Dictionary in pDictionary in python...
PPTX
Ch 7 Dictionaries 1.pptx
PDF
CHAPTER- 9 PYTHON DICTIONARIES.pdf computer science
PPTX
Untitled dictionary in python program .pdf.pptx
PPTX
Dictionaries in Python programming language
PDF
Dictionaries in Python programming for btech students
PDF
Dictionary part 1
PPTX
Python Fundamental Data structures: Dictionaries
PPTX
PYTHON Data structures Fundamentals: DICTIONARIES
PPTX
Python in easy way It includes different data type
PPTX
An Introduction To Python - Dictionaries
PPTX
6Dictionaries and sets in pythonsss.pptx
PPTX
Session10_Dictionaries.ppggyyyyyyyyyggggggggtx
PPTX
dictionary14 ppt FINAL.pptx
PDF
Python Dictionary
PPTX
Python programming –part 7
PPTX
Farhana shaikh webinar_dictionaries
python advanced data structure dictionary with examples python advanced data ...
Dictionaries.pptx
Python dictionaries
Dictionary in python Dictionary in python Dictionary in pDictionary in python...
Ch 7 Dictionaries 1.pptx
CHAPTER- 9 PYTHON DICTIONARIES.pdf computer science
Untitled dictionary in python program .pdf.pptx
Dictionaries in Python programming language
Dictionaries in Python programming for btech students
Dictionary part 1
Python Fundamental Data structures: Dictionaries
PYTHON Data structures Fundamentals: DICTIONARIES
Python in easy way It includes different data type
An Introduction To Python - Dictionaries
6Dictionaries and sets in pythonsss.pptx
Session10_Dictionaries.ppggyyyyyyyyyggggggggtx
dictionary14 ppt FINAL.pptx
Python Dictionary
Python programming –part 7
Farhana shaikh webinar_dictionaries
Ad

More from Emertxe Information Technologies Pvt Ltd (20)

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PPTX
Cloud computing and distributed systems.
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
cuic standard and advanced reporting.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Modernizing your data center with Dell and AMD
PPT
Teaching material agriculture food technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Big Data Technologies - Introduction.pptx
A Presentation on Artificial Intelligence
Cloud computing and distributed systems.
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
CIFDAQ's Market Insight: SEC Turns Pro Crypto
cuic standard and advanced reporting.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Network Security Unit 5.pdf for BCA BBA.
Per capita expenditure prediction using model stacking based on satellite ima...
Chapter 3 Spatial Domain Image Processing.pdf
Modernizing your data center with Dell and AMD
Teaching material agriculture food technology
NewMind AI Weekly Chronicles - August'25 Week I
MYSQL Presentation for SQL database connectivity
Digital-Transformation-Roadmap-for-Companies.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
The AUB Centre for AI in Media Proposal.docx
Big Data Technologies - Introduction.pptx

Python : Dictionaries

  • 2. Dictionaries Introduction Group of items arranged in the form of key-value pair Example d = {"Name": "Ram", "ID": 102, "Salary": 10000} Program #Print the entire dictionary print(d) #Print only the keys print("Keys in dic: ", d.keys()) #Print only values print("Values: ", d.values()) #Print both keys and value pairs as tuples print(d.items())
  • 3. Dictionaries Operations d = {"Name": "Ram", "ID": 102, "Salary": 10000} 1. To get the no. of pairs in the Dictionary n = len(d) 2. To modify the existing value d[salary] = 15000 3. To insert new key:value pair d["Dept"] = "Finance" 4. To delete the key:value pair del d["ID"] 5. To check whether the key is present in dictionary "Dept" in d - Returns True, if it is present 6. We can use any datatype fro values, but keys should obey the rules R1: Keys should be unique Ex: emp = {10: "Ram", 20: "Ravi", 10: "Rahim"} - Old value will be overwritten, emp = {10: "Rahim", 20: "Ravi"} R2: Keys should be immutable type. Use numbers, strings or tuples If mutable keys are used, will get 'TypeError'
  • 4. Dictionaries Methods clear() d.clear() Removes all key-value pairs from the d copy() d1 = d.copy() Copies all items from ‘d’ into a new dictionary ‘d1’ fromkeys() d.fromkeyss(s, [,v]) Create a new dictionary with keys from sequence ‘s’ and values all set to ‘v’ get() d.get(k, [,v]) Returns the value associated with key ‘k’. If key is not found, it returns ‘v’ items() d.items() Returns an object that contains key-value pairs of ‘d’. The pairs are stored as tuples in the object keys() d.keys() Returns a sequence of keys from the dictionary ‘d’ values() d.values() Returns a sequence of values from the dictionary ‘d’ update() d.update(x) Adds all elements from dictionary ‘x’ to ‘d’ pop() d.pop(k, [,v]) Removes the key ‘k’ and its value.
  • 5. Dictionaries Programs To create the dictionary with employee details d = {"Name": "Ram", "ID": 1023, "Salary": 10000} #Print the entire dictionary print(d) #Print only the keys print("Keys in dic: ", d.keys()) #Print only values print("Values: ", d.values()) #Print both keys and value pairs as tuples print(d.items())
  • 6. Dictionaries Programs #To create a dictionary from the keyboard and display the items x = {} print("Enter 'n' value: ", end='') n = int(input()) for i in range(n): print("Enter the key: ", end='') k = input() print("Enter the value: ", end='') v = int(input()) x.update({k: v}) print(x)
  • 7. Dictionaries Using for loop with Dictionaries Method-1 for k in colors: print(k) Method-2 for k in colors: print(colors[k]) Method-3 for k, v in colors.items(): print("key = {}nValue = {}". format(k, v))
  • 8. Dictionaries Sorting Dictionaries: Exercise To sort the elements of a dictionary based on akey or value
  • 9. Dictionaries Converting Lists into Dictionary Two step procedure - zip() - dict() #To convert list into dictionary countries = ["India", "USA"] cities = ["New Delhi", "Washington"] #Make a dictionary z = zip(countries, cities) d = dict(z) print(d)
  • 10. Dictionaries Converting strings into dictionary str = "Ram=23,Ganesh=20" #Create the empty list lst = [] for x in str.split(','): y = x.split('=') lst.append(y) #Convert into dictionary d = dict(lst) print(d)
  • 11. Dictionaries Passing dictionary to function By specifying the name of the dictionary as the parameter, we can pass the dictionary to the function. Example d = {10: "Ram"} display(d)
  • 12. Dictionaries Ordered Dictionaries from collections import OrderedDict Example d = {10: "Ram"} display(d) Program: #To create the ordered dictionary from collections import OrderedDict #Create empty dictionary d = OrderedDict() d[10] = 'A' d[11] = 'B' d[12] = 'C' d[13] = 'D' print(d)