(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to print a list of space-separated elements. Sample Solution Python Code: num = [1, 2, 3, 4, 5] print(*num) Sample Output: 1 2 3 4 5 Write a Python program to print a list of space-separated elements Free Machine …
Month: February 2021
(Python Example for Citizen Data Scientist & Business Analyst) Write a Python program to create a list of empty dictionaries. Sample Solution Python Code: n = 5 l = [{} for _ in range(n)] print(l) Sample Output: [{}, {}, {}, {}, {}] Write a Python program to create a list of empty …
(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 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 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 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 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 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 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 for Citizen Data Scientist & Business Analyst) Write a Python program to select the odd items of a list. Sample Solution Python Code: x = [1, 2, 3, 4, 5, 6, 7, 8, 9] print(x[::2]) Sample Output: [1, 3, 5, 7, 9] Write a Python program to select the odd …