Day: March 1, 2021

Python Example – Write a Python program to find the highest 3 values in a dictionary

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the highest 3 values in a dictionary.   Sample Solution: Python Code: from heapq import nlargest my_dict = {‘a’:500, ‘b’:5874, ‘c’: 560,’d’:400, ‘e’:5874, ‘f’: 20} three_largest = nlargest(3, my_dict, key=my_dict.get) print(three_largest) Sample Output: [‘e’, ‘b’, ‘c’]   Write a …

Python Example – Write a Python program to combine two dictionary adding values for common keys

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to combine two dictionary adding values for common keys.   Sample Solution: Python Code: from collections import Counter d1 = {‘a’: 100, ‘b’: 200, ‘c’:300} d2 = {‘a’: 300, ‘b’: 200, ‘d’:400} d = Counter(d1) + Counter(d2) print(d) Sample Output: Counter({‘b’: …

Python Example – Write a Python program to check a dictionary is empty or not

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check a dictionary is empty or not.   Sample Solution: Python Code: my_dict = {} if not bool(my_dict): print(“Dictionary is empty”) Sample Output: Dictionary is empty   Write a Python program to check a dictionary is empty or not Free …

Python Example – Write a Python program to remove duplicates from Dictionary

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to remove duplicates from Dictionary.   Sample Solution: Python Code: student_data = {‘id1’: {‘name’: [‘Sara’], ‘class’: [‘V’], ‘subject_integration’: [‘english, math, science’] }, ‘id2’: {‘name’: [‘David’], ‘class’: [‘V’], ‘subject_integration’: [‘english, math, science’] }, ‘id3’: {‘name’: [‘Sara’], ‘class’: [‘V’], ‘subject_integration’: [‘english, math, science’] …

Python Example – Write a Python program to get a dictionary from an object’s fields

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get a dictionary from an object’s fields.   Sample Solution: Python Code: class dictObj(object): def __init__(self): self.x = ‘red’ self.y = ‘Yellow’ self.z = ‘Green’ def do_nothing(self): pass test = dictObj() print(test.__dict__) Sample Output: {‘y’: ‘Yellow’, ‘z’: ‘Green’, ‘x’: ‘red’} …

Python Example – Write a Python program to get the maximum and minimum value in a dictionary

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the maximum and minimum value in a dictionary.   Sample Solution: Python Code: my_dict = {‘x’:500, ‘y’:5874, ‘z’: 560} key_max = max(my_dict.keys(), key=(lambda k: my_dict[k])) key_min = min(my_dict.keys(), key=(lambda k: my_dict[k])) print(‘Maximum Value: ‘,my_dict[key_max]) print(‘Minimum Value: ‘,my_dict[key_min]) Sample Output: …

Python Example – Write a Python program to sort a dictionary by key

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sort a dictionary by key.   Sample Solution: Python Code: color_dict = {‘red’:’#FF0000′, ‘green’:’#008000′, ‘black’:’#000000′, ‘white’:’#FFFFFF’} for key in sorted(color_dict): print(“%s: %s” % (key, color_dict[key])) Sample Output: black: #000000 green: #008000 red: #FF0000 white: #FFFFFF   Write a Python program …

Python Example – Write a Python program to map two lists into a dictionary

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to map two lists into a dictionary.   Sample Solution: Python Code: keys = [‘red’, ‘green’, ‘blue’] values = [‘#FF0000′,’#008000’, ‘#0000FF’] color_dictionary = dict(zip(keys, values)) print(color_dictionary) Sample Output: {‘green’: ‘#008000’, ‘blue’: ‘#0000FF’, ‘red’: ‘#FF0000’}   Write a Python program to map …

Python Example – Write a Python program to map two lists into a dictionary

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to map two lists into a dictionary.   Sample Solution:- Python Code: keys = [‘red’, ‘green’, ‘blue’] values = [‘#FF0000′,’#008000’, ‘#0000FF’] color_dictionary = dict(zip(keys, values)) print(color_dictionary) Sample Output: {‘green’: ‘#008000’, ‘blue’: ‘#0000FF’, ‘red’: ‘#FF0000’}   Write a Python program to map …

Python Example – Write a Python program to remove a key from a dictionary

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to remove a key from a dictionary.   Sample Solution: Python Code: myDict = {‘a’:1,’b’:2,’c’:3,’d’:4} print(myDict) if ‘a’ in myDict: del myDict[‘a’] print(myDict) Sample Output: {‘c’: 3, ‘b’: 2, ‘d’: 4, ‘a’: 1} {‘c’: 3, ‘b’: 2, ‘d’: 4}   Write …