Python Chapter9 Dictonary PDF
Python Chapter9 Dictonary PDF
Classes
YoganandSharma
y
is enclosed in curly braces. An empty dictionary
Training by
d
without any items is written with just two curly
JMD Study Computer
JM
not be. Accessing Values in Dictionary
To access dictionary elements, you can use the
familiar square brackets along with the key to obtain
its value.
Python Notes 1
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
+91-9928016573
Output:
dict['Age']: 7
J
dict['Name']: Zara
M
D
Stu
print ("dict['Age']: ", dict['Age'])
Python-Dictionar y
Following is a simple example −
d
print ("dict['Name']: ", dict['Name'])
y
Python Notes
dict= {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
2
Assistance Python-Dictionar y
Classes
YoganandSharma
Output:
tu
dict['Alice']:
S
D
Traceback(most recent call last):
File "test.py", line 4, in <module>
JM
print "dict['Alice']: ", dict['Alice'];
KeyError: 'Alice'
Updating Dictionary
You can update a dictionary by adding a new entry or a
key-value pair, modifying an existing entry, or deleting an
existing entry as shown below in the simple example −
Python Notes 3
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Dictionar y
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
y
Training
d
print ("dict['Age']: ", dict['Age'])
u
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
Output:
St
print ("dict['School']: ", dict['School'])
dict['Age']: 8
D
JM
dict['School']: DPS School
Delete Dictionary Elements
You can either remove individual dictionary elements or clear
the entire contents of a dictionary. You can also delete entire
dictionary in a single operation.
To explicitly remove an entire dictionary, just use the del
statement. Following is a simple example −
Python Notes 4
Placement Python-Dictionar y
Classes
YoganandSharma
y
Training by
tu
del dict; # delete entire dictionary
print ("dict['Age']: ", dict['Age'])
S
print ("dict['School']: ", dict['School'])
Output: D
dict['Age']:
JM
Traceback(most recent call last):
File "test.py", line 8, in <module>
print "dict['Age']: ", dict['Age'];
Python Notes 5
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Dictionar y
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
by Training.
+91-9928016573
y
Properties of Dictionary Keys
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
St
Dictionary values have no restrictions. They can be
any arbitrary Python object, either standard objects
D
or user-defined objects. However, same is not true
for the keys.
JM
Python Notes 6
Assistance Python-Dictionar y
Classes
YoganandSharma
y
Training by
Output:
d
JMD Study Computer
Live Project & 100%Job
dict['Name']: Manni
tu
S
(b) Keys must be immutable. Which means you can
D
use strings, numbers or tuples as dictionary keys but
JM
something like ['key'] is not allowed. Following is a
simple
Python Notes 7
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Dictionar y-functions
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Description
by Training.
dictionary.
y
Training
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
Syntax
t
Following is the syntax for len() method −
S
len(dict)
D
Parameters
JM
dict−This is the dictionary, whose length needs to be
calculated.
Return Value
This method returns the length.
Python Notes 8
Placement Python-Dictionar y-functions
Classes
YoganandSharma
Example
Training > Project >+91-9928016573
The following example shows the usage of len()
y
Training by
method.
d
JMD Study Computer
tu
dict= {'Name': 'Zara', 'Age': 7};
S
print ("Length : " len(dict))
D
When we run above program, it produces following
result −
JM
Length : 2
Python Notes 9
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Dictionar y-functions
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Description
by Training.
+91-9928016573
Syntax
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
t
Following is the syntax for str() method −
S
str(dict)
D
Parameters
JM
dict−This is the dictionary.
Return Value
This method returns string representation.
Python Notes 10
Assistance Python-Dictionar y-functions
Classes
YoganandSharma
Example
+91-9928016573
y
Training by
method.
d
JMD Study Computer
Live Project & 100%Job
tu
dict= {'Name': 'Zara', 'Age': 7};
S
print ("Equivalent String : " str(dict))
D
When we run above program, it produces following
result −
JM
Equivalent String : {'Age': 7, 'Name': 'Zara'}
Python Notes 11
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Dictionar y-functions
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Description
by Training.
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
Syntax
St
Following is the syntax for type() method −
type(dict) D
Parameters
JM
dict−This is the dictionary.
Return Value
This method returns the type of the passed variable.
Python Notes 12
Placement Python-Dictionar y-functions
Classes
YoganandSharma
Example
Training > Project >+91-9928016573
The following example shows the usage of type()
y
Training by
method.
d
JMD Study Computer
Python Notes 13
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.comYoganandSharma
+91-9928016573
NA
Syntax
dict.clear()
Description
Parameters
Return Value
JM
D
Study
Python Notes
Following is the syntax for clear() method −
Python-Dictionar y-Methods
The method clear() removes all items from the dictionary.
14
Assistance Python-Dictionar y-Methods
Classes
YoganandSharma
Example
The following example shows the usage of clear()
+91-9928016573
method.
y
Training by
d
JMD Study Computer
tu
print ("Start Len : %d" % len(dict))
S
dict.clear()
D
print ("End Len : %d" % len(dict))
Result
Output: JM
Start Len : 2
End Len : 0
Python Notes 15
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com YoganandSharma
+91-9928016573
NA
Syntax
dictionary.
dict.copy()
Description
Parameters
Return Value
JM
D
Study
Python Notes
Following is the syntax for copy() method −
The method copy() returns a shallow copy of the
Python-Dictionar y-Methods
16
Placement Python-Dictionar y-Methods
Classes
YoganandSharma
Training > Project >+91-9928016573
Example
y
Training by
tu
dict1 = {'Name': 'Manni', 'Age': 7, 'Class': 'First'}
dict2 = dict1.copy() S
D
print ("New Dictionary : ",dict2)
Result
Output: JM
New dictionary : {'Name': 'Manni', 'Age': 7, 'Class': 'First'}
Python Notes 17
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Dictionar y-Methods
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Description
by Training.
y
Training
Syntax
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
JM
dictionary keys preparation.
value −This is optional, if provided then value would be set to
this value
Return Value
This method returns the list.
Python Notes 18
Assistance Python-Dictionar y-Methods
Classes
YoganandSharma
Example
The following example shows the usage of fromkeys()
+91-9928016573
method.
y
Training by
dict= dict.fromkeys(seq)
tu
S
print ("New Dictionary : %s" % str(dict))
D
dict= dict.fromkeys(seq, 10)
JM
print ("New Dictionary : %s" % str(dict))
Result
Output:
New Dictionary : {'age': None, 'name': None, 'sex': None}
New Dictionary : {'age': 10, 'name': 10, 'sex': 10}
Python Notes 19
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Dictionar y-Methods
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Description
by Training.
The method get() returns a value for the given key. If key is not
+91-9928016573
y
Training
Syntax
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
JM
default −This is the Value to be returned in case key does not
exist.
Return Value
This method return a value for the given key. If key is not
available, then returns default value None.
Python Notes 20
Placement Python-Dictionar y-Methods
Classes
YoganandSharma
Example
Training > Project >+91-9928016573
d
JMD Study Computer
tu
dict= {'Name': 'Zara', 'Age': 27}
S
D
print ("Value : %s" % dict.get('Age'))
Result
Output: JM
print ("Value : %s" % dict.get('Sex', "NA"))
Value : 27
Value : NA
Python Notes 21
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Dictionar y-Methods
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Description
by Training.
+91-9928016573
Syntax
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
dict.has_key(key) St
Following is the syntax for has_key() method −
Parameters D
JM
key −This is the Key to be searched in the dictionary.
Return Value
This method return true if a given key is available in the
dictionary, otherwise it returns a false.
Python Notes 22
Assistance Python-Dictionar y-Methods
Classes
YoganandSharma
Example
+91-9928016573
d
dict= {'Name': 'Zara', 'Age': 7}
JMD Study Computer
Live Project & 100%Job
tu
print ("Value : %s" % dict.has_key('Age'))
S
print ("Value : %s" % dict.has_key('Sex'))
D
When we run the above program, it produces the
JM
following result −
Value : True
Value : False
Python Notes 23
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/ Live Project
Training by Training.
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com YoganandSharma
+91-9928016573
NA
Syntax
tuple pairs
Description
dict.items()
Parameters
Return Value
JM
D
Study
Python Notes
Following is the syntax for items() method −
Python-Dictionar y-Methods
The method items() returns a list of dict's(key, value)
24
Placement Python-Dictionar y-Methods
Classes
YoganandSharma
Example
Training > Project >+91-9928016573
The following example shows the usage of items()
y
Training by
method.
d
JMD Study Computer
Python Notes 25
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Dictionar y-Methods
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Description
by Training.
the dictionary.
Live Project
y
Training
Syntax
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
t
Following is the syntax for keys() method −
S
dict.keys()
Parameter D
s NA
JM
Return Value
This method returns a list of all the available keys in the
dictionary.
Python Notes 26
Assistance Python-Dictionar y-Methods
Classes
YoganandSharma
.
+91-9928016573
Example
y
Training by
Live Demo
tu
#!/usr/bin/python3
S
D
dict= {'Name': 'Zara', 'Age': 7}
JM
print ("Value : %s" % dict.keys())
Result
When we run above program, it produces the following result
−
Python Notes 27
Placement Python-Dictionar y-Methods
Classes
YoganandSharma
Description
The method setdefault() is similar to get(), but will set
Training > Project >+91-9928016573
dict[key] = default if key is not already in dict.
y
Training by
Syntax
d
JMD Study Computer
tu
Following is the syntax for setdefault() method −
S
dict.setdefault(key, default = None)
Parameters
D
key −This is the key to be searched.
JM
default −This is the Value to be returned in case key is
not found.
Return Value
This method returns the key value available in the
dictionary and if given key is not available then it will
return provided default value.
Python Notes 28
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Dictionar y-Methods
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
.Example
by Training.
+91-9928016573
method.
y
Training
ud
dict= {'Name': 'Zara', 'Age': 7}
print ("Value : %s" % dict.setdefault('Age', None))
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
print (dict) St
print ("Value : %s" % dict.setdefault('Sex', None))
Result D
Output:
JM
Value : 7
Value : None
{'Name': 'Zara', 'Sex': None, 'Age': 7}
Python Notes 29
Assistance Python-Dictionar y-Methods
Classes
YoganandSharma
Description
+91-9928016573
d
JMD Study Computer
Live Project & 100%Job
Syntax
tu
Following is the syntax for update() method −
S
D
dict.update(dict2)
Parameters
JM
dict2 −This is the dictionary to be added into dict.
Return Value
This method does not return any value.
Python Notes 30
Placement Python-Dictionar y-Methods
Classes
YoganandSharma
Example
Training > Project >+91-9928016573
The following example shows the usage of update()
method.
y
Training by
d
dict= {'Name': 'Zara', 'Age': 7}
JMD Study Computer
Python Notes 31
Web Designing, Digital Marketing, SEO, SMO, C, C++. Call: 9649141215, www.jmdstudy.com
Python-Dictionar y-Methods
Project Training in Python, Android, PHP, ASP.NET, Machine Learning, SQL, JAVA,
YoganandSharma
Description
by Training.
y
Training
Syntax
ud
MCA/ M.Tech/ B.Tech/ BCA Internship/ Industrial/
t
Following is the syntax for values() method −
S
dict.values()
Parameter D
s NA
JM
Return Value
This method returns a list of all the values available in a
given dictionary.
Python Notes 32
Assistance Python-Dictionar y-Methods
Classes
YoganandSharma
+91-9928016573
Exampl
y
Training by
e
Live Project & 100%Job
tu
method.
S
D
dict= {'Sex': 'female', 'Age': 7, 'Name': 'Zara'}
Result JM
print ("Values : ", list(dict.values()))
Output:
Python Notes 33