Month: February 2021

Python Example – Write a Python program to sort (ascending and descending) a dictionary by value

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sort (ascending and descending) a dictionary by value.   Sample Solution: Python Code: import operator d = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0} print(‘Original dictionary : ‘,d) sorted_d = dict(sorted(d.items(), key=operator.itemgetter(1))) print(‘Dictionary in ascending order by …

Python Example – Write a Python program to create a 3X3 grid with numbers

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create a 3X3 grid with numbers.   Sample Solution: Python Code: nums = [] for i in range(3): nums.append([]) for j in range(1, 4): nums[i].append(j) print(“3X3 grid with numbers:”) print(nums) Sample Output: 3X3 grid with numbers: [[1, 2, 3], [1, …

Python Example – Write a Python program to round every number of a given list of numbers

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to round every number of a given list of numbers and print the total sum multiplied by the length of the list.   Sample Solution: Python Code: nums = [22.4, 4.0, -16.22, -9.10, 11.00, -12.22, 14.20, -5.20, 17.50] print(“Original list: “, …

Python Example – Write a Python program to insert an element at a specified position into a given list.

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to insert an element at a specified position into a given list.   Sample Solution: Python Code: def insert_spec_position(x, n_list, pos): return n_list[:pos-1]+[x]+n_list[pos-1:] n_list = [1,1,2,3,4,4,5,1] print(“Original list:”) print(n_list) kth_position = 3 x = 12 result = insert_spec_position(x, n_list, kth_position) print(“nAfter …

Python Example – Write a Python program to extract a given number of randomly selected elements from a given list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to extract a given number of randomly selected elements from a given list.   Sample Solution: Python Code: import random def random_select_nums(n_list, n): return random.sample(n_list, n) n_list = [1,1,2,3,4,4,5,1] print(“Original list:”) print(n_list) selec_nums = 3 result = random_select_nums(n_list, selec_nums) print(“nSelected 3 …

Python Example – Write a Python program to split a given list into two parts where the length of the first part of the list is given

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to split a given list into two parts where the length of the first part of the list is given.   Sample Solution: Python Code: def split_two_parts(n_list, L): return n_list[:L], n_list[L:] n_list = [1,1,2,3,4,4,5, 1] print(“Original list:”) print(n_list) first_list_length = 3 …

Python Example – Write a Python program to pack consecutive duplicates of a given list elements into sublist

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to pack consecutive duplicates of a given list elements into sublists.   Sample Solution: Python Code: from itertools import groupby def pack_consecutive_duplicates(l_nums): return [list(group) for key, group in groupby(l_nums)] n_list = [0, 0, 1, 2, 3, 4, 4, 5, 6, 6, …

Python Example – Write a Python program to remove consecutive (following each other continuously) duplicates (elements) of a given list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to remove consecutive (following each other continuously) duplicates (elements) of a given list.   Sample Solution: Python Code: from itertools import groupby def compress(l_nums): return [key for key, group in groupby(l_nums)] n_list = [0, 0, 1, 2, 3, 4, 4, 5, …

Python Example – Write a Python program to flatten a given nested list structure

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to flatten a given nested list structure.   Sample Solution: Python Code: def flatten_list(n_list): result_list = [] if not n_list: return result_list stack = [list(n_list)] while stack: c_num = stack.pop() next = c_num.pop() if c_num: stack.append(c_num) if isinstance(next, list): if next: …

Python Example – Write a Python program to flatten a given nested list structure

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to flatten a given nested list structure.   Sample Solution: Python Code: def flatten_list(n_list): result_list = [] if not n_list: return result_list stack = [list(n_list)] while stack: c_num = stack.pop() next = c_num.pop() if c_num: stack.append(c_num) if isinstance(next, list): if next: …