AmitDiwan has Published 10744 Articles

Python Program to check whether all elements in a string list are numeric

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:19:57

3K+ Views

When it is required to check wether all elements in a list of strings are numeric, the ‘all’ operator is used.ExampleBelow is a demonstration of the samemy_list = ["434", "823", "98", "74", '9870'] print("The list is :") print(my_list) my_result = all(ele.isdigit() for ele in my_list) if(my_result == ... Read More

Python Program to replace elements of a list based on the comparison with a number

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:17:32

444 Views

When it is required to replace elements of a list based on the comparison with a number, a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [32, 37, 44, 38, 92, 61, 28, 92, 20] print("The list is :") print(my_list) my_key = 32 print("The key ... Read More

Python Program to Extract Rows of a matrix with Even frequency Elements

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:15:08

225 Views

When it is required to extract rows of a matrix with even frequency elements, a list comprehension with ‘all’ operator and ‘Counter’ method are used.ExampleBelow is a demonstration of the samefrom collections import Counter my_list = [[41, 25, 25, 62], [41, 41, 41, 41, 22, 22], [65, 57, 65, ... Read More

Python Program to convert a list into matrix with size of each row increasing by a number

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:13:11

432 Views

When it is required to convert a list into matrix with the size of every row increasing by a number, the ‘//’ operator and a simple iteration is used.ExampleBelow is a demonstration of the samemy_list = [42, 45, 67, 89, 99, 10, 23, 12, 31, 43, 60, 1, 0] ... Read More

Python program to sort tuples by frequency of their absolute difference

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:10:49

311 Views

When it is required to sort tuples by frequency of their absolute difference, the lambda function, the ‘abs’ method and the ‘sorted’ method are used.ExampleBelow is a demonstration of the samemy_list = [(11, 26), (21, 33), (90, 11), (26, 21), (32, 18), (25, 37)] print("The list is :") print(my_list) ... Read More

Python Pandas – Get the datatype and DataFrame columns information

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:10:31

238 Views

To get the datatype and DataFrame columns information, use the info() method. Import the required library with an alias −import pandas as pd;Create a DataFrame with 3 columns −dataFrame = pd.DataFrame(    {       "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'], "Place": ['Delhi', 'Bangalore', 'Hyderabad', 'Chandigarh', 'Pune', ... Read More

Python Program to Remove First Diagonal Elements from a Square Matrix

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:09:00

203 Views

When it is required to remove the first diagonal elements from a square matrix, the ‘enumerate’ and list comprehension is used.ExampleBelow is a demonstration of the samemy_list = [[45, 67, 85, 42, 11], [78, 99, 10, 13, 0], [91, 23, 23, 64, 23], [91, 11, 22, 14, 35]] print("The ... Read More

Python Program to Extract Strings with at least given number of characters from other list

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:07:26

142 Views

When it is required to extract strings with atleast a given number of characters from the other list, a list comprehension is used.ExampleBelow is a demonstration of the samemy_list = ["Python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) my_char_list = ['e', 't', 's', 'm', 'n'] ... Read More

Python program to Convert Matrix to Dictionary Value List

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:05:43

438 Views

When it is required to convert a matrix to a dictionary value list, a simple dictionary comprehension can be used.ExampleBelow is a demonstration of the samemy_list = [[71, 26, 35], [65, 56, 37], [89, 96, 99]] print("The list is :") print(my_list) my_result = {my_index + 1 : my_list[my_index] ... Read More

Python program to randomly create N Lists of K size

AmitDiwan

AmitDiwan

Updated on 16-Sep-2021 08:00:28

234 Views

When it is required to create N lists randomly that are K in size, a method is defined that shuffles the values and yields the output.ExampleBelow is a demonstration of the samefrom random import shuffle def gen_random_list(my_val, K): while True: ... Read More

Advertisements