Programming Articles

Page 329 of 2547

Python Program to print strings based on the list of prefix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 479 Views

When it is required to print strings based on the list of prefix elements, a list comprehension, the any() operator and the startswith() method are used. Example Below is a demonstration of the same ? my_list = ["streek", "greet", "meet", "leeks", "mean"] print("The list is:") print(my_list) prefix_list = ["st", "ge", "me", "re"] print("The prefix list is:") print(prefix_list) my_result = [element for element in my_list if any(element.startswith(ele) for ele in prefix_list)] print("The result is:") print(my_result) Output The list is: ['streek', 'greet', 'meet', 'leeks', 'mean'] The prefix list is: ['st', ...

Read More

Python Pandas - Convert Nested Dictionary to Multiindex Dataframe

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 2K+ Views

Converting a nested dictionary to a Pandas MultiIndex DataFrame involves restructuring the dictionary keys as hierarchical column indices. This creates a DataFrame with multi-level column headers that represent the nested structure. Creating a Nested Dictionary First, let's create a nested dictionary with sports data − import pandas as pd # Create nested dictionary nested_dict = { 'Cricket': { 'Boards': ['BCCI', 'CA', 'ECB'], 'Country': ['India', 'Australia', 'England'] }, ...

Read More

Python prorgam to remove duplicate elements index from other list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 316 Views

When working with two lists, you may need to remove elements from the second list at positions where duplicate values occur in the first list. This can be accomplished using enumerate(), sets for tracking duplicates, and list comprehension. Problem Understanding Given two lists of the same length, we want to: Find duplicate elements in the first list Get the indices where these duplicates occur Remove elements from the second list at those duplicate indices Example Here's how to remove elements from the second list based on duplicate indices from the first list − ...

Read More

Python Program to remove a specific digit from every element of the list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 604 Views

When you need to remove a specific digit from every element of a list, you can use string manipulation with list comprehension and filtering techniques. Example Below is a demonstration of removing digit 3 from all list elements ? numbers = [123, 565, 1948, 334, 4598] print("The list is:") print(numbers) digit_to_remove = 3 print("The digit to remove is:") print(digit_to_remove) result = [] for element in numbers: # Convert to string and filter out the specific digit filtered_digits = ''.join([digit for digit in str(element) if ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 3K+ Views

When working with string lists, you often need to check whether all elements contain only numeric characters. Python provides the all() function combined with isdigit() method to accomplish this task efficiently. Using all() and isdigit() The all() function returns True if all elements in an iterable are true, and isdigit() checks if a string contains only digits ? my_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: print("All the elements in the list are numeric") else: ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 292 Views

When working with matrices (lists of lists), we sometimes need to extract rows where each element appears an even number of times. This can be achieved using list comprehension with the all() function and Counter from the collections module. Understanding Even Frequency A row has "even frequency elements" when every unique element in that row appears an even number of times (2, 4, 6, etc.). For example: [1, 1, 2, 2] − Element 1 appears 2 times, element 2 appears 2 times (both even) [3, 3, 3, 4] − Element 3 appears 3 times (odd), so ...

Read More

Python Pandas – Display all the column names in a DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

To display all the column names in a Pandas DataFrame, use the DataFrame.columns attribute. This returns an Index object containing all column names. Creating a DataFrame First, let's create a sample DataFrame ? import pandas as pd # Create a DataFrame dataFrame = pd.DataFrame({ "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'], "Place": ['Delhi', 'Bangalore', 'Hyderabad', 'Chandigarh', 'Pune', 'Mumbai', 'Jaipur'], "Units": [100, 150, 50, 110, 90, 120, 80] }) print("DataFrame:") print(dataFrame) DataFrame: ...

Read More

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

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 505 Views

When it is required to convert a list into a matrix with the size of every row increasing by a number, the floor division operator // and list slicing are used together with iteration. Example Below is a demonstration of the same − my_list = [42, 45, 67, 89, 99, 10, 23, 12, 31, 43, 60, 1, 0] print("The list is :") print(my_list) my_key = 3 print("The value of key is") print(my_key) my_result = [] for index in range(0, len(my_list) // my_key): my_result.append(my_list[0: (index + 1) * my_key]) ...

Read More

Python program to sort tuples by frequency of their absolute difference

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 392 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. This technique sorts tuples based on how often their absolute difference appears in the dataset. Understanding Absolute Difference The absolute difference between two numbers is the positive difference between them. For a tuple (a, b), the absolute difference is abs(a - b). Example Below is a demonstration of sorting tuples by frequency of their absolute difference ? my_list = [(11, 26), (21, 33), (90, 11), (26, 21), (32, 18), ...

Read More

Python Program to Remove First Diagonal Elements from a Square Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 286 Views

When it is required to remove the first diagonal elements from a square matrix, the enumerate function and list comprehension can be used. The first diagonal (main diagonal) consists of elements where the row index equals the column index. Understanding the Main Diagonal In a square matrix, the main diagonal contains elements at positions (0, 0), (1, 1), (2, 2), and so on. These are the elements we need to remove ? Matrix with Main Diagonal ...

Read More
Showing 3281–3290 of 25,466 articles
« Prev 1 327 328 329 330 331 2547 Next »
Advertisements