Python

Python Example – Write a Python program to split a list based on first character of word

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to split a list based on first character of word. Sample Input: word_list = [‘be’,’have’,’do’,’say’,’get’,’make’,’go’,’know’,’take’,’see’,’come’,’think’, ‘look’,’want’,’give’,’use’,’find’,’tell’,’ask’,’work’,’seem’,’feel’,’leave’,’call’]   Sample Solution Python Code: from itertools import groupby from operator import itemgetter word_list = [‘be’,’have’,’do’,’say’,’get’,’make’,’go’,’know’,’take’,’see’,’come’,’think’, ‘look’,’want’,’give’,’use’,’find’,’tell’,’ask’,’work’,’seem’,’feel’,’leave’,’call’] for letter, words in groupby(sorted(word_list), key=itemgetter(0)): print(letter) for …

Python Example – Write a Python program to convert a list of multiple integers into a single integer

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert a list of multiple integers into a single integer.   Sample Solution Python Code: L = [11, 33, 50] print(“Original List: “,L) x = int(“”.join(map(str, L))) print(“Single Integer: “,x) Sample Output: Original List: [11, 33, 50] Single Integer: 113350 …

Python Example – Write a Python program to find common items from two lists

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find common items from two lists.   Sample Solution Python Code: color1 = “Red”, “Green”, “Orange”, “White” color2 = “Black”, “Green”, “White”, “Pink” print(set(color1) & set(color2)) Sample Output: {‘Green’, ‘White’}   Write a Python program to find common items from …

Python Example – Write a Python program to generate all sublists of a list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to generate all sublists of a list.   Sample Solution Python Code: from itertools import combinations def sub_lists(my_list): subs = [] for i in range(0, len(my_list)+1): temp = [list(x) for x in combinations(my_list, i)] if len(temp)>0: subs.extend(temp) return subs l1 = …

Python Example – Write a Python program to count the number of elements in a list within a specified range

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to count the number of elements in a list within a specified range.   Sample Solution Python Code: def count_range_in_list(li, min, max): ctr = 0 for x in li: if min <= x <= max: ctr += 1 return ctr list1 …

Python Example – Write a Python program to get the frequency of the elements in a list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the frequency of the elements in a list.   Sample Solution Python Code: import collections my_list = [10,10,10,10,20,20,20,20,40,40,50,50,30] print(“Original List : “,my_list) ctr = collections.Counter(my_list) print(“Frequency of the elements in the List : “,ctr) Sample Output: Original List : …

Python Example – Write a Python program to get unique values from a list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get unique values from a list.   Sample Solution Python Code: my_list = [10, 20, 30, 40, 20, 50, 60, 40] print(“Original List : “,my_list) my_set = set(my_list) my_new_list = list(my_set) print(“List of unique numbers : “,my_new_list) Sample Output: Original …

Python Example – Write a Python program to find the second largest number in a list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the second largest number in a list.   Sample Solution Python Code: def second_largest(numbers): if (len(numbers)<2): return if ((len(numbers)==2) and (numbers[0] == numbers[1]) ): return dup_items = set() uniq_items = [] for x in numbers: if x not in …

Python Example – Write a Python program to find the second smallest number in a list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the second smallest number in a list.   Sample Solution Python Code: def second_smallest(numbers): if (len(numbers)<2): return if ((len(numbers)==2) and (numbers[0] == numbers[1]) ): return dup_items = set() uniq_items = [] for x in numbers: if x not in …