Python

Python Example – Write a Python program to find a tuple, the smallest second index value from a list of tuples

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find a tuple, the smallest second index value from a list of tuples.   Sample Solution Python Code: x = [(4, 1), (1, 2), (6, 0)] print(min(x, key=lambda n: (n[1], -n[0]))) Sample Output: (6, 0)   Write a Python program …

Python Example – Write a Python program to check whether the n-th element exists in a given list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check whether the n-th element exists in a given list.   Sample Solution Python Code: x = [1, 2, 3, 4, 5, 6] xlen = len(x)-1 print(x[xlen]) Sample Output: 6   Write a Python program to check whether the n-th …

Python Example – Write a Python program to check whether all items of a list is equal to a given string

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to check whether all items of a list is equal to a given string.   Sample Solution Python Code: color1 = [“green”, “orange”, “black”, “white”] color2 = [“green”, “green”, “green”, “green”] print(all(c == ‘blue’ for c in color1)) print(all(c == ‘green’ …

Python Example – Write a Python program to convert a string to a list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert a string to a list.   Sample Solution Python Code: import ast color =”[‘Red’, ‘Green’, ‘White’]” print(ast.literal_eval(color)) Sample Output: [‘Red’, ‘Green’, ‘White’]   Write a Python program to convert a string to a list Free Machine Learning & Data …

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’: [], …