Tag Archives: python example

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

(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 & Data …

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

(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 program …

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

(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 insert a …

Python Example – Write a Python program to print a list of space-separated elements

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

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 …