0% found this document useful (0 votes)
11 views

Dictionary Methods

cs class 12 dict notes

Uploaded by

Yash Rao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Dictionary Methods

cs class 12 dict notes

Uploaded by

Yash Rao
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Dictionary

Methods/Functions
len( ) :
This function returns the length of Dictionary means, it returns the total number of Key-Value pairs
present in the dictionary.

for example:

A = {1 : "One", 2 : "Two", 3 : "Three"}


B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
print("Length of dictionary A is :", len(A))
print("Length of dictionary B is :", len(B))

OUTPUT
Length of dictionary A is : 3
Length of dictionary B is : 4
clear( ) :
This function simply removes all items from the dictionary. for example

A = {1 : "One", 2 : "Two", 3 : "Three"}


B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
A.clear()
print("Length of dictionary A is :", len(A))
print("Length of dictionary B is :", len(B))

OUTPUT
Length of dictionary A is : 0
Length of dictionary B is : 4

NOTE : This function only remove element/items from the dictionary. It does not delete the
dictionary
get( ) :
This function simply return the value of the required Key. for example

A = {1 : "One", 2 : "Two", 3 : "Three"}


B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
print(A.get(2))
print(B.get('C'))
print(A.get(4)) #This key is not available in dictionary A so return NONE
print(A.get(4),"Key Not Found") #We can specify our message to display if key not found

OUTPUT
Two
Cat
None
Key Not Found

NOTE: If key is not available in dictionary then this method


will return None or customized message as shown above
items( ) :
This function returns all the key value pair of dictionary in the form of list of tuples. for
example

A = {1 : "One", 2 : "Two", 3 : "Three"}


B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
print(A.items())
print(B.items())

OUTPUT
dict_items([(1, 'One'), (2, 'Two'), (3, 'Three')])
dict_items([('A', 'Apple'), ('B', 'Bat'), ('C', 'Cat'), ('D', 'Doll')])
keys( ) :
This function return all the keys of dictionary in the form of List. for example :

A = {1 : "One", 2 : "Two", 3 : "Three"}


B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
print(A.keys())
print(B.keys())

OUTPUT:
dict_keys([1, 2, 3])
dict_keys(['A', 'B', 'C', 'D'])
values( ):
This function return all the values of dictionary in the form of List. for example :

A = {1 : "One", 2 : "Two", 3 : "Three"}


B = {'A' : "Apple", 'B' : "Bat", 'C' : "Cat", 'D' : "Doll"}
print(A.values())
print(B.values())

OUTPUT:
dict_values(['One', 'Two', 'Three'])
dict_values(['Apple', 'Bat', 'Cat', 'Doll'])
update( ):
This function merge the key value air of two dictionary into one. In simple words it simply
join/merge the two dictionary. It merge the keys and values of one dictionary into other and
overwrites the values of the same key. for example:

A = {1 : "One", 2 : "Two", 3 : "Three"}


B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
A.update(B)
print(A)

OUTPUT

{1: 'Amit', 2: 'Sunil', 3: 'Three', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}

#It over writes the values of same keys and add the values of different keys
pop( ):
This function not only delete the element of required key but also return the deleted value.
B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}
a=B.pop(2) #It returns the element of Key - 2
print(a)
print(B)

OUTPUT

Sunil {1: 'Amit', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}


_________________________________________________________________________________

B = {1: 'Amit', 2: 'Sunil', 5: 'Lata', 6: 'Suman', 7: 'Ravi'}


a=B.pop(6) #It returns the element of Key - 6 print(a)
print(B)

OUTPUT

Suman {1: 'Amit', 2: 'Sunil', 5: 'Lata', 7: 'Ravi'}


More Built-in Dictionary Methods
S.No Method & Description
.
1 setdefault() method returns the value of the x = dict(name = “teena", country = "India")
item with the specified key. If the key does not y=x.setdefault('age',39)
exist, insert the key, with the specified value. print(y)
OUTPUT-> 39

2 max() – returns key having maximum value Tv = {'a':100, 'b':1292, 'c' : 88}
Keymax = max(Tv, key=Tv.get)
print(Keymax)
OUTPUT-> b
3 min()- returns key having minimum value
4 sorted- sort by key or value dict1 = {'b':100, 'a':12, 'c' : 88}
y = sorted(dict1.items(), reverse=True)
OUTPUT-> [('b', 100), ('c', 88), ('a', 12)] print(y)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy