Dictionary:
A dictionary is a built-in data structure that stores data in key-value pairs.
A dictionary is a collection which is ordered, changeable and do not allow duplicates.
Dictionary is represented by {}.
menu = {'idli':10, 'dosai':20, 'poori':30}
print(menu)
menu['pongal'] = 40
print(menu)
del menu['idli']
print(menu)
print(menu['dosai'])
{'idli': 10, 'dosai': 20, 'poori': 30}
{'idli': 10, 'dosai': 20, 'poori': 30, 'pongal': 40}
{'dosai': 20, 'poori': 30, 'pongal': 40}
20
get():
Returns the value for a given key, and returns None if the key doesn't exist.
time_table = {}
time_table['tamil'] = 10
time_table['english']= 10
print(time_table)
print(time_table['tamil'])
print(time_table.get('tamil'))
print(time_table.get('maths'))
print(time_table['maths'])
{'tamil': 10, 'english': 10}
10
10
None
KeyError: 'maths'
keys(): Returns a view object that displays all the keys.
values(): Returns a view object that displays all the values.
items(): Returns a view object that displays a list of a dictionary's key-value tuple pairs.
menu = {'idli':10, 'dosai':20, 'poori':30}
print(menu)
print(menu.keys())
print(menu.values())
print(menu.items())
{'idli': 10, 'dosai': 20, 'poori': 30}
dict_keys(['idli', 'dosai', 'poori'])
dict_values([10, 20, 30])
dict_items([('idli', 10), ('dosai', 20), ('poori', 30)])
fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
for fruit in fruits_menu.keys():
print(fruit)
for price in fruits_menu.values():
print(price)
for fruit, price in fruits_menu.items():
print(fruit, price)
apple
banana
grapes
100
80
120
apple 100
banana 80
grapes 120
Write a Program to print the keys that key contains letter "e":
fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
for fruit in fruits_menu.keys():
if 'e' in fruit:
print(fruit)
apple
grapes
Write a Program to print the keys and values that key contains letter "e":
fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
for fruit,price in fruits_menu.items():
if 'e' in fruit:
print(fruit,price)
apple 100
grapes 120
Write a Program to print the values that key contains letter "e":
fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
for fruit in fruits_menu.keys():
if 'e' in fruit:
print(fruits_menu[fruit])
100
120
To convert dictionary into tuples and list.
fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
print(list(fruits_menu))
print(tuple(fruits_menu))
print(list(fruits_menu.keys()))
print(list(fruits_menu.values()))
print(tuple(fruits_menu.keys()))
print(tuple(fruits_menu.values()))
['apple', 'banana', 'grapes']
('apple', 'banana', 'grapes')
['apple', 'banana', 'grapes']
[100, 80, 120]
('apple', 'banana', 'grapes')
(100, 80, 120)
Nested Dictionary:
emp1 = {'name':'guru prasanna', 'qual':'B.Com'}
emp2 = {'name':'lakshmi pritha', 'qual': 'M.E'}
print(emp1)
print(emp2)
employees = {101:emp1, 102:emp2}
print(employees)
{'name': 'guru prasanna', 'qual': 'B.Com'}
{'name': 'lakshmi pritha', 'qual': 'M.E'}
{101: {'name': 'guru prasanna', 'qual': 'B.Com'}, 102: {'name': 'lakshmi pritha', 'qual': 'M.E'}}
Write a program to get employees name from the dictionary
emp1 = {'name':'guru prasanna', 'qual':'B.Com'}
emp2 = {'name':'lakshmi pritha', 'qual': 'M.E'}
employees = {101:emp1, 102:emp2}
for roll_no, employee in employees.items():
for key, value in employee.items():
if key == 'name':
print(employee[key])
guru prasanna
lakshmi pritha
Write a program to get M.E employee name from the dictionary
emp1 = {'name':'guru prasanna', 'qual':'B.Com'}
emp2 = {'name':'lakshmi pritha', 'qual': 'M.E'}
employees = {101:emp1, 102:emp2}
for roll_no, employee in employees.items():
for key, value in employee.items():
if value == 'M.E':
print(employee['name'])
lakshmi pritha
Write a program to increase 10% of each values.
fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
for fruit in fruits_menu.values():
fruit=fruit+(fruit/10)
print(fruit)
110.0
88.0
132.0
Write a program to convert key into value and value into key.
fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
new_menu = {}
fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
for fruit,price in fruits_menu.items():
new_menu[price] = fruit
print(new_menu)
{100: 'apple', 80: 'banana', 120: 'grapes'}
Dictionary comprehension:
fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
menu_dict = {(fruit,price) for fruit,price in fruits_menu.items()}
print(menu_dict)
menu_dict = {fruit: price for fruit,price in fruits_menu.items()}
print(menu_dict)
menu_dict = {price : fruit for fruit,price in fruits_menu.items()}
print(menu_dict)
{('banana', 80), ('apple', 100), ('grapes', 120)}
{'apple': 100, 'banana': 80, 'grapes': 120}
{100: 'apple', 80: 'banana', 120: 'grapes'}
fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
print(fruits_menu.get('apple',"not available"))
print(fruits_menu.get('kivi',"not available"))
100
not available
Write a program to find frequency of letters in a given string.
freq = {}
name = 'lakshmipritha'
for letter in name:
freq[letter] = freq.get(letter,0)+1
print(freq)
{'l': 1, 'a': 2, 'k': 1, 's': 1, 'h': 2, 'm': 1, 'i': 2, 'p': 1, 'r': 1, 't': 1}
Write a program to convert dictionary into set.
csk = {'dhoni':101, 'jadeja':102}
india = {'virat':103, 'jadeja':102}
print(set(csk))
print(set(india))
print(set(csk.keys()))
print(set(india.keys()))
{'jadeja', 'dhoni'}
{'jadeja', 'virat'}
{'jadeja', 'dhoni'}
{'jadeja', 'virat'}
setdefault():
If the key exists: Returns the current value of the key.
If the key does not exist: Adds the key with the specified default value and returns the default value.
csk = {'dhoni':101, 'jadeja':102}
india = {'virat':103, 'jadeja':102}
csk.setdefault('rohit',100)
print(csk)
csk.setdefault('dhoni',100)
print(csk)
{'dhoni': 101, 'jadeja': 102, 'rohit': 100}
{'dhoni': 101, 'jadeja': 102, 'rohit': 100}
Task:
- Find: a) Common in both the teams b) Present in any one of the teams c) Total Players names
csk = {'dhoni':101, 'jadeja':102}
india = {'virat':103, 'jadeja':102}
csk_set=set(csk)
india_set=set(india)
print(csk_set & india_set)
print(csk_set ^ india_set)
print(csk_set | india_set)
{'jadeja'}
{'dhoni', 'virat'}
{'dhoni', 'jadeja', 'virat'}
Write a program to find frequency of words in a string- 'a rose is a rose is a rose'.
sentence = "a rose is a rose is a rose"
words = sentence.split()
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
print(freq)
{'a': 3, 'rose': 3, 'is': 2}
Write a program to find total,average and high score from the given dictionary.
#total,average
players = {'jaiswal':75, 'rohit':55, 'virat':95}
total=0
for score in players.values():
total=total+score
average=total/len(players)
print(total)
print(average)
225
75.0
#highest score
players = {'jaiswal':75, 'rohit':55, 'virat':95}
player=list(players.values())
highest = player[0]
for score in player:
if score>highest:
highest = score
print(highest)
95
Top comments (0)