Day: February 26, 2021

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

Python Example – Write a Python program to check whether all dictionaries in a list are empty or not

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check whether all dictionaries in a list are empty or not.   Sample Solution: Python Code: my_list = [{},{},{}] my_list1 = [{1,2},{},{}] print(all(not d for d in my_list)) print(all(not d for d in my_list1)) Sample Output: True False   Write …

Python Example – Write a Python program to get the depth of a dictionary

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the depth of a dictionary.   Sample Solution Python Code: def dict_depth(d): if isinstance(d, dict): return 1 + (max(map(dict_depth, d.values())) if d else 0) return 0 dic = {‘a’:1, ‘b’: {‘c’: {‘d’: {}}}} print(dict_depth(dic)) Sample Output: 4   Write …

Python Example – Write a Python program to remove duplicates from a list of lists

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to remove duplicates from a list of lists.   Sample Solution Python Code: import itertools num = [[10, 20], [40], [30, 56, 25], [10, 20], [33], [40]] print(“Original List”, num) num.sort() new_num = list(num for num,_ in itertools.groupby(num)) print(“New List”, new_num) …