Tag Archives: python for beginners

Python Example – Write a Python program to replace dictionary values with their average

Hits: 1 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to replace dictionary values with their average.   Sample Solution: Python Code: def sum_math_v_vi_average(list_of_dicts): for d in list_of_dicts: n1 = d.pop(‘V’) n2 = d.pop(‘VI’) d[‘V+VI’] = (n1 + n2)/2 return list_of_dicts student_details= [ {‘id’ : 1, ‘subject’ : ‘math’, …

Python Example – Write a Python program to create a dictionary from two lists without losing duplicate values

Hits: 4 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create a dictionary from two lists without losing duplicate values.   Sample Solution: Python Code: from collections import defaultdict class_list = [‘Class-V’, ‘Class-VI’, ‘Class-VII’, ‘Class-VIII’] id_list = [1, 2, 2, 3] temp = defaultdict(set) for c, i in …

Python Example – Write a Python program to sort Counter by value

Hits: 4 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sort Counter by value.   Sample Solution: Python Code: from collections import Counter x = Counter({‘Math’:81, ‘Physics’:83, ‘Chemistry’:87}) print(x.most_common()) Sample Output: [(‘Chemistry’, 87), (‘Physics’, 83), (‘Math’, 81)]   Write a Python program to sort Counter by value Free …

Python Example – Write a Python program to count number of items in a dictionary value that is a list

Hits: 4 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to count number of items in a dictionary value that is a list.   Sample Solution: Python Code: dict = {‘Alex’: [‘subj1’, ‘subj2’, ‘subj3’], ‘David’: [‘subj1’, ‘subj2’]} ctr = sum(map(len, dict.values())) print(ctr) Sample Output: 5   Write a Python …

Python Example – Write a Python program to check multiple keys exists in a dictionary

Hits: 8 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check multiple keys exists in a dictionary.   Sample Solution: Python Code: student = { ‘name’: ‘Alex’, ‘class’: ‘V’, ‘roll_id’: ‘2’ } print(student.keys() >= {‘class’, ‘name’}) print(student.keys() >= {‘name’, ‘Alex’}) print(student.keys() >= {‘roll_id’, ‘name’}) Sample Output: True False …

Python Example – Write a Python program to print a dictionary line by line

Hits: 6 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to print a dictionary line by line.   Sample Solution: Python Code: students = {‘Aex’:{‘class’:’V’, ‘rolld_id’:2}, ‘Puja’:{‘class’:’V’, ‘roll_id’:3}} for a in students: print(a) for b in students[a]: print (b,’:’,students[a][b]) Sample Output: Aex class : V rolld_id : 2 Puja …

Python Example – Write a Python program to get the key value and item in a dictionary

Hits: 4 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the key, value and item in a dictionary.   Sample Solution: Python Code: dict_num = {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60} print(“key value count”) for count, (key, value) in enumerate(dict_num.items(), 1): …

Python Example – Write a Python program to sort a list alphabetically in a dictionary

Hits: 7 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sort a list alphabetically in a dictionary.   Sample Solution: Python Code: num = {‘n1’: [2, 3, 1], ‘n2’: [5, 1, 2], ‘n3’: [3, 2, 4]} sorted_dict = {x: sorted(y) for x, y in num.items()} print(sorted_dict) Sample Output: …

Python Example – Write a Python program to count the values associated with key in a dictionary

Hits: 8 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to count the values associated with key in a dictionary.   Sample Solution: Python Code: student = [{‘id’: 1, ‘success’: True, ‘name’: ‘Lary’}, {‘id’: 2, ‘success’: False, ‘name’: ‘Rabi’}, {‘id’: 3, ‘success’: True, ‘name’: ‘Alex’}] print(sum(d[‘success’] for d in …

Python Example – Write a Python program to print a dictionary in table format

Hits: 0 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to print a dictionary in table format.   Sample Solution: Python Code: my_dict = {‘C1′:[1,2,3],’C2′:[5,6,7],’C3’:[9,10,11]} for row in zip(*([key] + (value) for key, value in sorted(my_dict.items()))): print(*row) Sample Output: C1 C2 C3 1 5 9 2 6 10 3 …

Python Example – Write a Python program to combine values in python list of dictionaries

Hits: 0 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to combine values in python list of dictionaries.   Sample Solution: Python Code: from collections import Counter item_list = [{‘item’: ‘item1’, ‘amount’: 400}, {‘item’: ‘item2’, ‘amount’: 300}, {‘item’: ‘item1’, ‘amount’: 750}] result = Counter() for d in item_list: result[d[‘item’]] …

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

Hits: 6 (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’]   …

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

Hits: 2 (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 …

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

Hits: 7 (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 …

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

Hits: 4 (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, …

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

Hits: 3 (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’, …