DEV Community

Lakshmi Pritha Nadesan
Lakshmi Pritha Nadesan

Posted on

3

Day-29 Dictionary and Tasks

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'])
Enter fullscreen mode Exit fullscreen mode
{'idli': 10, 'dosai': 20, 'poori': 30}
{'idli': 10, 'dosai': 20, 'poori': 30, 'pongal': 40}
{'dosai': 20, 'poori': 30, 'pongal': 40}
20
Enter fullscreen mode Exit fullscreen mode

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'])

Enter fullscreen mode Exit fullscreen mode
{'tamil': 10, 'english': 10}
10
10
None
KeyError: 'maths'
Enter fullscreen mode Exit fullscreen mode

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())
Enter fullscreen mode Exit fullscreen mode
{'idli': 10, 'dosai': 20, 'poori': 30}
dict_keys(['idli', 'dosai', 'poori'])
dict_values([10, 20, 30])
dict_items([('idli', 10), ('dosai', 20), ('poori', 30)])
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode
apple
banana
grapes
100
80
120
apple 100
banana 80
grapes 120

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
apple
grapes

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
apple 100
grapes 120
Enter fullscreen mode Exit fullscreen mode

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])

Enter fullscreen mode Exit fullscreen mode
100
120
Enter fullscreen mode Exit fullscreen mode

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()))
Enter fullscreen mode Exit fullscreen mode
['apple', 'banana', 'grapes']
('apple', 'banana', 'grapes')
['apple', 'banana', 'grapes']
[100, 80, 120]
('apple', 'banana', 'grapes')
(100, 80, 120)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
{'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'}}

Enter fullscreen mode Exit fullscreen mode

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])

Enter fullscreen mode Exit fullscreen mode
guru prasanna
lakshmi pritha
Enter fullscreen mode Exit fullscreen mode

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'])

Enter fullscreen mode Exit fullscreen mode
lakshmi pritha
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode
110.0
88.0
132.0
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
{100: 'apple', 80: 'banana', 120: 'grapes'}

Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode
{('banana', 80), ('apple', 100), ('grapes', 120)}
{'apple': 100, 'banana': 80, 'grapes': 120}
{100: 'apple', 80: 'banana', 120: 'grapes'}

Enter fullscreen mode Exit fullscreen mode
fruits_menu = {'apple':100, 'banana':80, 'grapes':120}
print(fruits_menu.get('apple',"not available"))
print(fruits_menu.get('kivi',"not available"))

Enter fullscreen mode Exit fullscreen mode
100
not available
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
{'l': 1, 'a': 2, 'k': 1, 's': 1, 'h': 2, 'm': 1, 'i': 2, 'p': 1, 'r': 1, 't': 1}
Enter fullscreen mode Exit fullscreen mode

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()))

Enter fullscreen mode Exit fullscreen mode
{'jadeja', 'dhoni'}
{'jadeja', 'virat'}
{'jadeja', 'dhoni'}
{'jadeja', 'virat'}
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
{'dhoni': 101, 'jadeja': 102, 'rohit': 100}
{'dhoni': 101, 'jadeja': 102, 'rohit': 100}

Enter fullscreen mode Exit fullscreen mode

Task:

  1. 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)

Enter fullscreen mode Exit fullscreen mode
{'jadeja'}
{'dhoni', 'virat'}
{'dhoni', 'jadeja', 'virat'}

Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode
{'a': 3, 'rose': 3, 'is': 2}
Enter fullscreen mode Exit fullscreen mode

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)

Enter fullscreen mode Exit fullscreen mode
225
75.0
Enter fullscreen mode Exit fullscreen mode
#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)

Enter fullscreen mode Exit fullscreen mode
95
Enter fullscreen mode Exit fullscreen mode

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay