Month: February 2021

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Python Example – Write a Python program to find all the values in a list are greater than a specified number

Hits: 84 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find all the values in a list are greater than a specified number.   Sample Solution Python Code: list1 = [220, 330, 500] list2 = [12, 17, 21] print(all(x >= 200 for x in list1)) print(all(x >= 25 …

Python Example – Write a Python program to find the list in a list of lists whose sum of elements is the highest

Hits: 112 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the list in a list of lists whose sum of elements is the highest.   Sample Solution Python Code: num = [[1,2,3], [4,5,6], [10,11,12], [7,8,9]] print(max(num, key=sum)) Sample Output: [10, 11, 12]   Write a Python program …

Python Example – Write a Python program to access dictionary key’s element by index

Hits: 6 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to access dictionary key’s element by index.   Sample Solution Python Code: num = {‘physics’: 80, ‘math’: 90, ‘chemistry’: 86} print(list(num)[0]) Sample Output: physics   Write a Python program to access dictionary key’s element by index Free Machine Learning …

Python Example – Write a Python program to iterate over two lists simultaneously

Hits: 5 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to iterate over two lists simultaneously.   Sample Solution Python Code: num = [1, 2, 3] color = [‘red’, ‘white’, ‘black’] for (a,b) in zip(num, color): print(a, b) Sample Output: 1 red 2 white 3 black   Write a …

Python Example – Write a Python program to access dictionary key’s element by index

Hits: 13 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to access dictionary key’s element by index.   Sample Solution Python Code: num = {‘physics’: 80, ‘math’: 90, ‘chemistry’: 86} print(list(num)[0]) Sample Output: physics   Write a Python program to access dictionary key’s element by index Free Machine Learning …

Python Example – Write a Python program to iterate over two lists simultaneously

Hits: 17 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to iterate over two lists simultaneously.   Sample Solution Python Code: num = [1, 2, 3] color = [‘red’, ‘white’, ‘black’] for (a,b) in zip(num, color): print(a, b) Sample Output: 1 red 2 white 3 black   Write a …

Python Example – Write a Python program to insert a given string at the beginning of all items in a list

Hits: 96 (Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to insert a given string at the beginning of all items in a list.   Sample Solution Python Code: num = [1,2,3,4] print([’emp{0}’.format(i) for i in num]) Sample Output: [’emp1′, ’emp2′, ’emp3′, ’emp4′]   Write a Python program to …