Day: February 25, 2021

Python Example – Write a Python program to remove key values pairs from a list of dictionaries

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to remove key values pairs from a list of dictionaries.   Sample Solution Python Code: original_list = [{‘key1′:’value1’, ‘key2′:’value2’}, {‘key1′:’value3’, ‘key2′:’value4’}] print(“Original List: “) print(original_list) new_list = [{k: v for k, v in d.items() if k != ‘key1’} for d in …

Python Example – Write a Python program to concatenate elements of a list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to concatenate elements of a list.   Sample Solution Python Code: color = [‘red’, ‘green’, ‘orange’] print(‘-‘.join(color)) print(”.join(color)) Sample Output: red-green-orange redgreenorange   Write a Python program to concatenate elements of a list Free Machine Learning & Data Science Coding Tutorials …

Python Example – Write a Python program to sort a list of nested dictionaries

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to sort a list of nested dictionaries.   Sample Solution Python Code: my_list = [{‘key’: {‘subkey’: 1}}, {‘key’: {‘subkey’: 10}}, {‘key’: {‘subkey’: 5}}] print(“Original List: “) print(my_list) my_list.sort(key=lambda e: e[‘key’][‘subkey’], reverse=True) print(“Sorted List: “) print(my_list) Sample Output: Original List: [{‘key’: {‘subkey’: …

Python Example – Write a Python program to split a list into different variables

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to split a list into different variables.   Sample Solution Python Code: color = [(“Black”, “#000000”, “rgb(0, 0, 0)”), (“Red”, “#FF0000”, “rgb(255, 0, 0)”), (“Yellow”, “#FFFF00”, “rgb(255, 255, 0)”)] var1, var2, var3 = color print(var1) print(var2) print(var3) Sample Output: (‘Black’, ‘#000000’, …

Python Example – Write a Python program to create multiple lists

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to create multiple lists.   Sample Solution Python Code: obj = {} for i in range(1, 21): obj[str(i)] = [] print(obj) Sample Output: {‘1’: [], ‘8’: [], ’14’: [], ‘5’: [], ’17’: [], ‘9’: [], ‘2’: [], ‘7’: [], ’16’: [], …

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