Month: March 2021

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 …