Python

Python Example – Write a python program to check whether two lists are circularly identical

(Python Example for Citizen Data Scientist & Business Analyst)   Write a python program to check whether two lists are circularly identical.   Sample Solution Python Code: list1 = [10, 10, 0, 0, 10] list2 = [10, 10, 10, 0, 0] list3 = [1, 10, 10, 0, 0] print(‘Compare list1 and list2’) print(‘ ‘.join(map(str, list2)) …

Python Example – Write a Python program to select an item randomly from a list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to select an item randomly from a list.   Sample Solution Python Code: import random color_list = [‘Red’, ‘Blue’, ‘Green’, ‘White’, ‘Black’] print(random.choice(color_list)) Sample Output: Black   Write a Python program to select an item randomly from a list Free Machine …

Python Example – Write a Python program to append a list to the second list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to append a list to the second list.   Sample Solution Python Code: list1 = [1, 2, 3, 0] list2 = [‘Red’, ‘Green’, ‘Black’] final_list = list1 + list2 print(final_list) Sample Output: [1, 2, 3, 0, ‘Red’, ‘Green’, ‘Black’]   Write …

Python Example – Write a Python program to flatten a shallow list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to flatten a shallow list.   Sample Solution Python Code: import itertools original_list = [[2,4,3],[1,5,6], [9], [7,9,0]] new_merged_list = list(itertools.chain(*original_list)) print(new_merged_list) Sample Output: [2, 4, 3, 1, 5, 6, 9, 7, 9, 0]   Write a Python program to flatten a …

Python Example – Write a Python program to find the index of an item in a specified list

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to find the index of an item in a specified list.   Sample Solution Python Code: num =[10, 30, 4, -6] print(num.index(30)) Sample Output: 1   Write a Python program to find the index of an item in a specified list …

Python Example – Write a Python program to convert a list of characters into a string

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to convert a list of characters into a string.   Sample Solution Python Code: s = [‘a’, ‘b’, ‘c’, ‘d’] str1 = ”.join(s) print(str1) Sample Output: abcd     Free Machine Learning & Data Science Coding Tutorials in Python & R …

Python Example – Write a Python program to get the difference between the two lists

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to get the difference between the two lists.   Sample Solution Python Code: list1 = [1, 3, 5, 7, 9] list2=[1, 2, 4, 6, 7, 8] diff_list1_list2 = list(set(list1) – set(list2)) diff_list2_list1 = list(set(list2) – set(list1)) total_diff = diff_list1_list2 + diff_list2_list1 …

Python Example – Write a Python program to generate all permutations of a list in Python

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to generate all permutations of a list in Python. In mathematics, the notion of permutation relates to the act of arranging all the members of a set into some sequence or order, or if the set is already ordered, rearranging (reordering) …

Python Example – Write a Python program to generate and print a list of first and last 5 elements

(Python Example for Citizen Data Scientist & Business Analyst)   Write a Python program to generate and print a list of first and last 5 elements where the values are square of numbers between 1 and 30 (both included).   Sample Solution Python Code: def printValues(): l = list() for i in range(1,21): l.append(i**2) print(l[:5]) …