Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python-08

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Dictionaries

(organized collection of data)


XI-IP/CS
Working with Dictionary
• Creating a Dictionary
• Traversing a Dictionary
• Accessing keys or values
• Characteristics of a Dictionary
• Dictionary manipulation (Add, insert, Update
delete etc)
• Pretty Printing a Dictionary (formatting)
• Dictionary functions & Methods
Creating a Dictionary
Dictionaries are mutable data type, unordered
collections with elements in the form of a key :
value pairs that associate keys to values.
Dictionaries are also called associative arrays or
mappings.
<Dictionary name> = { <key> : <value>, ….}
e.g. Output :
MonthDays = {“Jan” : 31, “Feb” : 28, “Mar” : 31 ….}
Key Value
Jan 31
Feb 28
Creating a Dictionary
• Internally, dictionaries are indexed on the basis
of keys.
• The keys of a dictionary must be of immutable
types.
• Dictionaries are also called associative arrays or
hashes.
• A Dictionary operation that takes a key and
finds the corresponding value is called lookup.
Traversing a Dictionary
Accessing an processing each element of
dictionary is known as traversing. (similar to
python loop)
e.g. MonthDays = {“Jan” : 31, “Feb” : 28, “Mar” :
31, “Apr” : 30, ….}
for <item> in <Dictionary> :
Process element
for key in MonthDays :
print(key, MonthDays[key])
Accessing keys or values
Keys and values or a dictionary can be access
separately.
e.g. MonthDays = {“Jan” : 31, “Feb” : 28, “Mar” :
31, “Apr” : 30, ….}
<Dictionary>.keys() and <Dictionary>.values()
list(MonthDays.keys())
[“Jan”, “Feb”, “Mar”, …]
list(MonthDays.values())
[31, 28, 31, …]
Characteristics of a Dictionary
1. Unordered Set
2. Not a Sequence
3. Indexed by keys, not numbers
4. Keys must be unique
5. Dictionaries are mutable
6. Internally stored as mappings
Working with Dictionaries
Initializing a Dictionary:
The dict() method is used to construct dictionary.
e.g. D=dict()
or D = { }
or D = {‘Amit’ : 50, ‘Nidhi’: 43}
Creating Dictionary from keys and values:
e.g. key= [‘A’,’B’,’C’]
value=[1,2,3]
D=dict(zip(key,value))
Working with Dictionary
Adding an Element:
To add new element to a dictionary use (key:value)
pair. e.g. <Dictionary>[key]=<value>
D[‘C’]=8
Deleting an Element:
DEL command is used to delete dictionary
element.
e.g. DEL D[‘B’]
DEL D
Working with Dictionary
Searching :
Membership operators in and not in used to
search in dictionary.
<key> in <dictionary>
<key> not in <dictionary>
e.g. D1= {‘name’ : ‘Rajesh’, ‘age’ : 23}
‘Rajesh’ in D1 ?
‘Ramesh’ not in D1 ?
‘age’ in D1 ?
23 in D1 ?
Working with Dictionary
Pretty Printing in Dictionary:
Print function is use to display content of a
dictionary.
e.g. D1= {‘name’ : ‘Rajesh’, ‘age’ : 23}
print(D1) ?
dumps() function:
This method is used to display indented output of
a dictionary.
Use import json for dumps function
e.g. Json.dumps(<dict>,indent=<n>)
Dictionary methods and funcitons
Len method:
This method is used to count the number of
elements in the dictionary.
len(<dict>)
e.g. Len(D1)
?
clear method:
This method is used to remove all the elements
from the dictionary.
e.g. D1.clear()
Dictionary methods and funcitons
Update() method:
This method is used to add new elements in the
dictionary.
<dict1>.update(<dict2>)
e.g. D1.update(D2)
get() method:
This method is used to get the item the given key
from the dictionary.
<dict1>.get(key)
e.g. D1.get(‘name’)
Dictionary methods and funcitons
items() method:
This method returns all of the elements in the
dictionary as a sequence of (key, value).
e.g. A= D1.items()
Keys() method:
To get all the keys of the dictionary.
e.g. D1.keys() ?
Values() method:
To get all the values of the dictionary.
e.g. D1.values() ?
Exercise
1. Create a dictionary using dict() function.
2. Write code to create dictionary using lists.
3. WAP to convert dictionary to lists.
4. WAP to identify the position of string in the
given dictionary.
5. WAP for find and replace value of a key.
Tuples
Tuples: Tuples as those lists which cannot be
changed (immutable). Tuples are represented as list
of comma separated values of any data type within
parenthesis.
e.g. T = (“Amit”, 21, ’XI’, 80.5)
a=() empty tuple
b=(‘phy’, ’maths’, ‘chem’, ‘CS’) string tuple
Creating Tuples
Empty tuple : a=()
a= tuple()
Single element tuple : a=(8)

Tuple from list : b=[‘phy’, ’maths’, ‘chem’, ‘CS’]


a = tuple(b)
Tuple using input method :
a=tuple(input(‘Enter tuple elements:’))
Note: for single digit or character
Creating Tuples
Tuple using eval () method :
a=tuple(eval(‘Enter tuple elements:’))

S= “BalBharati”
N=34,56,34,6
L=[4,6,3,6,7]
Accessing Tuples
Index method :
A tuple can be access through index same as list.
Tuple elements are also indexed forward and
backward indexing.
Tuple[start : end]
e.g. T[4]
t[2:5]

You might also like