Dictionary
Dictionary
○ Mohan: 95
○ Ram: 89
○ Suhel: 92
○ Sangeeta: 85
● Method 2
>>> for key,value in dict1.items():
print(key,':',value)
○ Mohan: 95
○ Ram: 89
○ Suhel: 92
○ Sangeeta: 85
○
Dictionary Methods and Built-in functions
● fromkeys(): It is used to create a new dictionary from a sequence
containing all the keys and a common value, which will be assigned to all
the keys
Syntax: dict.fromkeys(<key sequence>,[<value>])
<key sequence> is a python sequence containing the keys for the new
dictionary
<value> is the common value that will be assigned to all the keys
Example:
>>>n=dict.fromkeys([2,4,6,8],100)
>>>n
{2:100,4:100,6:100,8:100}
>>>n1=dict.fromkeys((3,4,5))
>>>n1
{3:None,4:None,5:None}
>>>n2=dict.fromkeys((3,4,5),(6,7,8))
>>>n2
{3:(6,7,8),4:(6,7,8),5:(6,7,8)}
● setdefault() - It inserts a new key:value pair only if the key doesn’t already
exists. If key already exists, it returns the current value of the key
Syntax: dict.setdefault(<key>,<value>)
Example:
>>>marks={1:89,2:75,3:78,4:57}
>>>marks.setdefault(5,98)
98
>>>marks.setdefault(3,85)
78
Marks
{1:89,2:75,3:78,4:57,5:98}
● copy(): it creates a copy of dictionary
● If the values referenced by the keys are immutable, then any changes made in the
copy created with copy() will not be reflected in the original dictionary as the
copied dictionary has own set referencing keys
>>> std={1:’neha’,2:’saima’,3:’anvit’,4:’ana’}
>>>std1=std.copy()
>>>std1
{1:’neha’,2:’saima’,3:’anvit’,4:’ana’}
>>>std1[3]=‘kavita’
>>>std1
{1:’neha’,2:’saima’,3:’kavita’,4:’ana’}
>>>std
{1:’neha’,2:’saima’,3:’anvit’,4:’ana’}
● pop() method : this method removes and returns the dictionary element associated to passed key
Syntax: dict.pop(key,<value>)
<dict> is the dictionary in which key is to be deleted
<key> is the key to be deleted
<value> is the return value/message which will be returned by method
Example:
>>>s={1: [34, 56], 2: [98, 65, 33], 3: [86, 90]}
>>>s.pop(3)
[86, 90]
>>>s
{1: [34, 56], 2: [98, 65, 33]}
>>>s.pop(2)
[98, 65, 33]
>>>s.pop(1,'not found')
[34, 56]
>>>s.pop(2,'key not found')
'key not found'
: This method removes and returns last item entered the
● popitem()
sorted(<dict>,[reverse=False])
<dict> is the dictionary whose keys are sorted
Reverse argument is optional and when set to True, it returns the keys of
dictionary in descending order. default value of reverse is false
Example
>>>std={1: 'komal',2: 'sai',3: 'hari',4: 'karthik',5: 'shiva',7: 'shivani',8: ‘veena'}
>>>sorted(std)
[1, 2, 3, 4, 5, 7, 8]
>>>sorted(std,reverse=True)
[8, 7, 5, 4, 3, 2, 1]
>>>sorted(std.values())
['hari', 'karthik', 'komal', 'sai', 'shiva', 'shivani', 'veena']
>>>sorted(std.items())
[(1, 'komal'),(2, 'sai'),(3, 'hari'),(4, 'karthik'),(5, 'shiva'),(7, 'shivani'),(8,
'veena')]
>>>sorted(std.keys())
[1, 2, 3, 4, 5, 7, 8]
● max() : This method returns the maximum key value of the given
dictionary
Syntax:
max(<dict>)
<dict> is the dictionary name
Example:
>>>std={1: 'komal',2: 'sai',3: 'hari',4: 'karthik',5: 'shiva',7:
'shivani',8: ‘veena'}
>>>max(std)
8
● min() : This method returns the minimum key value from the given
dictionary
syntax:
min(<dict>
<dict> is the dictionary name
example:
>>>std={1: 'komal',2: 'sai',3: 'hari',4: 'karthik',5: 'shiva',7: 'shivani',8:
‘veena’}
>>>max(std)
1
● Sum() : This method returns sum of keys in the given dictionary
● Syntax:
○ Sum(<dict>)
○ <dict> is the dictionary name
example:
>>>std={1: 'komal',2: 'sai',3: 'hari',4: 'karthik',5: 'shiva',7: 'shivani',8: ‘veena’}
>>>sum(std)
30
Programs
● Count the number of times a character appears in a given string using a dictionary
str=input("enter string:")
char_count=dict()
count=0
for i in str:
if i in char_count:
char_count[i]+=1
else:
char_count[i]=1
print(char_count)
Programs
● Count the number of times a character appears in a given string using a dictionary
str=input("enter string:")
char_count=dict()
count=0
for i in str:
if i in char_count:
char_count[i]+=1
else:
char_count[i]=1
print(char_count)
● create a dictionary with names of employees, their salary and access them
emp=dict()
n=int(input("enter number of employees data you want to store: "))
for i in range(n):
name=input("enter name of employee")
sal=input("enter salary of employee")
emp[name]=sal
for n,s in emp.items():
print(n,s)