0% found this document useful (0 votes)
24 views22 pages

Data Structure - Dictionary - Session10

Learn something
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views22 pages

Data Structure - Dictionary - Session10

Learn something
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Data Structure - Dictionary

Session -10
Dictionary
We can use List, Tuple and Set to represent a group of individual objects as a
single entity.

If we want to represent a group of objects as key-value pairs then we should


go for Dictionary.

Eg:

rollno----name
phone number--address
ipaddress---domain name
• Duplicate keys are not allowed but values can be duplicated.
• Heterogeneous objects are allowed for both key and values.
• Insertion order is not preserved.
• Dictionaries are mutable.
• Dictionaries are dynamic indexing and slicing concepts are not applicable.

Note: In C++ and Java Dictionaries are known as "Map" where as in Perl and
Ruby it is known as "Hash"
How to create Dictionary ?
d={} or d=dict()

we are creating empty dictionary. We can add entries as follows

d[100]="raman"
d[200]="ravi"
d[300]="shiva"
print(d) #{100: 'raman', 200: 'ravi', 300: 'shiva’}

If we know data in advance then we can create dictionary as follows

d={100:'raman' ,200:'ravi', 300:'shiva'}


d={key:value, key:value}
How to access data from the dictionary?

We can access data by using keys.

d={100:'raman' ,200:'ravi', 300:'shiva'}

print(d[100])
print(d[300])
If the specified key is not available then we will get KeyError

print(d[400])

We can prevent this by checking whether key is already available or not by


using has_key() function or by using in operator.

d.has_key(400) ==> returns 1 if key is available otherwise returns 0

But has_key() function is available only in Python 2 but not in Python 3.

Hence,compulsory we have to use in operator.

if 400 in d:
print(d[400])
Write a program to enter name and percentage marks in a dictionary and
display information on the screen

How to update dictionaries?

d[key]=value

If the key is not available then a new entry will be added to the dictionary
with the specified key-value pair

If the key is already available then old value will be replaced with new value.
Eg:

d={100:"raman",200:"ravi",300:"shiva"}
print(d)
d[400]="tom"
print(d)
d[100]="jerry"
print(d)
How to delete elements from dictionary?

del d[key]

It deletes entry associated with the specified key.


If the key is not available then we will get KeyError

Eg:
d={100:"raman",200:"ravi",300:"shiva"}
print(d)
del d[100]
print(d)
del d[400]

d.clear()
To remove all entries from the dictionary

Eg:

d={100:"raman",200:"ravi",300:"shiva"}
print(d)
d.clear()
print(d)

del d
To delete total dictionary. Now we cannot access d
Eg:

d={100:"raman",200:"ravi",300:"shiva"}
print(d)
del d
print(d)
Important functions of dictionary:

1. dict():

To create a dictionary

d=dict() ===>It creates empty dictionary

2. len():

Returns the number of items in the dictionary

3. clear():

To remove all elements from the dictionary


4. get():

To get the value associated with the key d.get(key)

If the key is available then returns the corresponding value otherwise returns
None.

It wont raise any error.

d.get(key,defaultvalue)
If the key is available then returns the corresponding value otherwise returns
default value.

d={100:"durga",200:"ravi",300:"shiva"}
print(d[100])
#print(d[400])
print(d.get(100))
print(d.get(400))
print(d.get(100,"Guest"))
print(d.get(400,"Guest"))
5. pop():

d.pop(key)

It removes the entry associated with the specified key and returns the
corresponding value.
If the specified key is not available then we will get KeyError

Eg:

d={100:"raman",200:"ravi",300:"shiva"}
print(d.pop(100))
print(d)
print(d.pop(400))
6. popitem():

It removes an arbitrary item(key-value) from the dictionaty and returns it.

Eg:

d={100:"raman",200:"ravi",300:"shiva"}
print(d)
print(d.popitem())
print(d)

If the dictionary is empty then we will get KeyError

d={}
print(d.popitem()) ==>KeyError: 'popitem(): dictionary is empty'
7. keys():

It returns all keys associated eith dictionary

d={100:"raman",200:"ravi",300:"shiva"}
print(d.keys())
for k in d.keys():
print(k)
8. values():

It returns all values associated with the dictionary

d={100:"raman",200:"ravi",300:"shiva"}
print(d.values())
for v in d.values():
print(v)
9. items():

It returns list of tuples representing key-value pairs.

[(k,v),(k,v),(k,v)]

Eg:

d={100:"raman",200:"ravi",300:"shiva"}
for k,v in d.items():
print(k,"--",v)
10. copy():

To create exactly duplicate dictionary(cloned copy)

d1=d.copy();

11. setdefault():

d.setdefault(k,v)

If the key is already available then this function returns the corresponding
value.

If the key is not available then the specified key-value will be added as new
item to the dictionary.
12. update():

d.update(x)

All items present in the dictionary x will be added to dictionary d


THANK YOU

You might also like