Table of Contents
Dictionaries are unordered compound datatypes of python.There are defined using {}. Following are python programs to show use and structure of dictionary datatype.
1. (Sort based on value)Write a Python program to sort (ascending and descending) a dictionary by value.
2. (Sort based on key) Write a Python program to sort (ascending and descending) a dictionary by key.
3.Write a Python program to concatenate following dictionaries to create a new one. Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
4.Write a Python program to sum all the items in a dictionary.
In below example sorted(reverse=False) method is used,where if reverse is set to True than dictionary will be sorted in descending order else by default reverse is always False i.e. ascending order
Practical 5a (Sort based on value): Write a Python program to sort (ascending and descending) a dictionary by value.
""" Author : ITVoyagers (https://itvoyagers.in) Date :11th August 2019 Description : Write a Python program to sort (ascending and descending) a dictionary by value. """ """operator module exports a set of functions implemented in C corresponding to the intrinsic operators of Python.""" import operator itvoyagers = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} print('Original dictionary : ',itvoyagers) sorted_itvoyagers = sorted(itvoyagers.items(), key=operator.itemgetter(1)) print('Dictionary in ascending order by value : ',sorted_itvoyagers) sorted_itvoyagers = sorted(itvoyagers.items(), key=operator.itemgetter(1),reverse=True) #operator.itemgetter(1) will sort based on dictionary values print('Dictionary in descending order by value : ',sorted_itvoyagers)
OUTPUT
>>> Original dictionary : {0: 0, 1: 2, 2: 1, 3: 4, 4: 3} Dictionary in ascending order by value : [(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)] Dictionary in descending order by value : [(3, 4), (4, 3), (1, 2), (2, 1), (0, 0)]
NOTE :
1. Operator module exports a set of functions implemented in C corresponding to the intrinsic operators of Python.
2. Class itemgetter return a callable object that fetches the given item(s) from its operand.
After f = itemgetter(2), the call f(r) returns r[2].
After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])
3. items() returns both key and value pair in list of tuples.

EXAMPLE
>>> itvoyagers = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} >>> itvoyagers.items() dict_items([(0, 0), (1, 2), (2, 1), (3, 4), (4, 3)]) >>>
Practical 5a (Sort based on key): Write a Python program to sort (ascending and descending) a dictionary by key.
""" Author : ITVoyagers (https://itvoyagers.in) Date :11th August 2019 Description : Write a Python program to sort (ascending and descending) a dictionary by key. """ """operator module exports a set of functions implemented in C corresponding to the intrinsic operators of Python.""" import operator itvoyagers = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} print('Original dictionary : ',itvoyagers) sorted_itvoyagers = sorted(itvoyagers.items(), key=operator.itemgetter(0)) print('Dictionary in ascending order by value : ',sorted_itvoyagers) sorted_itvoyagers = sorted(itvoyagers.items(), key=operator.itemgetter(0),reverse=True) #operator.itemgetter(1) will sort based on dictionary keys print('Dictionary in descending order by value : ',sorted_itvoyagers)
OUTPUT
>>> Original dictionary : {0: 0, 1: 2, 2: 1, 3: 4, 4: 3} Dictionary in ascending order by value : [(0, 0), (1, 2), (2, 1), (3, 4), (4, 3)] Dictionary in descending order by value : [(4, 3), (3, 4), (2, 1), (1, 2), (0, 0)] >>>
Practical 5b : Write a Python program to concatenate following dictionaries to create a new one. Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
""" Author : ITVoyagers (https://itvoyagers.in) Date :11th August 2019 Description : Write a Python program to concatenate following dictionaries to create a new one. Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}
""" itv1={1:10, 2:20} itv2={3:30, 4:40} itv3={5:50,6:60} itv4 = {} #empty dictionary for itv in (itv1, itv2, itv3): itv4.update(itv) print(itv4)
OUTPUT
>>> {1:10, 2:20, 3: 30, 4: 40, 5: 50, 6: 60}
Practical 5c : Write a Python program to sum all the items in a dictionary.
""" Author : ITVoyagers (https://itvoyagers.in) Date :11th August 2019 Description : Write a Python program to sum all the items in a dictionary. """ my_dict = {'a':1000,'b':-5000,'c':1000} print(sum(my_dict.values())) #values() is used to access value only from dict
OUTPUT
>>> -3000 >>>