Programming Articles

Page 352 of 2547

Python program to print Rows where all its Elements' frequency is greater than K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 239 Views

When working with nested lists (2D arrays), you may need to filter rows where every element appears more than K times within that row. Python provides an elegant solution using the all() function combined with list comprehension. Understanding the Problem We want to find rows where each element's frequency within that specific row exceeds a threshold value K. For example, in row [11, 11, 11, 11], the element 11 appears 4 times, so its frequency is 4. Solution Using Custom Function Here's a complete program that defines a helper function to check frequency conditions ? ...

Read More

Python Program to repeat elements at custom indices

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 162 Views

When working with lists, you might need to repeat certain elements at specific positions. This can be achieved using Python's enumerate() function along with the extend() and append() methods to create a new list with duplicated elements at custom indices. Basic Approach Using enumerate() The most straightforward method is to iterate through the original list and check if the current index should have its element repeated ? original_list = [34, 56, 77, 23, 31, 29, 62, 99] print("The original list is:") print(original_list) indices_to_repeat = [3, 1, 4, 6] result = [] for index, element ...

Read More

Python – Inverse Dictionary Values List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 423 Views

Inverting a dictionary means swapping keys and values. When the original dictionary has lists as values, we create a new dictionary where each item in those lists becomes a key, and the original keys become the values. Using defaultdict The defaultdict from collections module automatically creates empty lists for new keys ? from collections import defaultdict my_dict = {13: [12, 23], 22: [31], 34: [21], 44: [52, 31]} print("The original dictionary is:") print(my_dict) inverted_dict = defaultdict(list) for key, values in my_dict.items(): for value in values: ...

Read More

Python – Sort String list by K character frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 434 Views

When it is required to sort a list of strings based on the K character frequency, the sorted() method and lambda function can be used. This technique counts how many times a specific character appears in each string and sorts accordingly. Syntax sorted(iterable, key=lambda string: -string.count(character)) Example Below is a demonstration of sorting strings by character frequency ? string_list = ['Hi', 'Will', 'Jack', 'Python', 'Bill', 'Mills', 'goodwill'] print("Original list:") print(string_list) K = 'l' print(f"Sorting by character '{K}' frequency:") # Sort by K character frequency (descending order) result = sorted(string_list, key=lambda ...

Read More

Pandas GroupBy – Count the occurrences of each combination

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

When analyzing data, we often need to count how many times each combination of values appears across multiple columns. In Pandas, we can use DataFrame.groupby() with size() to count occurrences of each unique combination. Creating a Sample DataFrame Let's start by creating a DataFrame with car sales data ? import pandas as pd # Create sample data data = { 'Car': ['BMW', 'Mercedes', 'Lamborghini', 'Audi', 'Mercedes', 'Porsche', 'RollsRoyce', 'BMW'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Bangalore', 'Hyderabad', 'Mumbai', 'Mumbai', 'Delhi'], 'Sold': [95, 80, 80, ...

Read More

Python Program to extracts elements from a list with digits in increasing order

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 222 Views

When it is required to extract elements from a list with digits in increasing order, a simple iteration, a flag value and the str method is used. This technique helps identify numbers where each digit is larger than the previous one. Example Let's extract numbers that have digits in strictly increasing order ? my_list = [4578, 7327, 113, 3467, 1858] print("The list is :") print(my_list) my_result = [] for element in my_list: my_flag = True for index in range(len(str(element)) - 1): ...

Read More

Python – Dual Tuple Alternate summation

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 240 Views

When it is required to perform dual tuple alternate summation, a simple iteration and the modulus operator are used. This technique alternates between summing the first element of tuples at even indices and the second element of tuples at odd indices. Below is a demonstration of the same − Example my_list = [(24, 11), (45, 66), (53, 52), (77, 51), (31, 10)] print("The list is :") print(my_list) my_result = 0 for index in range(len(my_list)): if index % 2 == 0: my_result += ...

Read More

Python – Extract Rear K digits from Numbers

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 277 Views

When it is required to extract rear K digits from numbers, a simple list comprehension, the modulo operator and the ** operator are used. The modulo operator with 10**K effectively extracts the last K digits from any number. How It Works The formula number % (10**K) extracts the rear K digits because: 10**K creates a number with K+1 digits (e.g., 10³ = 1000) The modulo operation returns the remainder when dividing by this value This remainder is always less than 10**K, giving us exactly the last K digits Example Let's extract the last ...

Read More

Python – Combine list with other list elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 252 Views

When it is required to combine list with other list elements, Python provides several methods. The most common approaches are using iteration with append(), the extend() method, or the + operator. Method 1: Using Loop with append() This method iterates through the second list and appends each element to the first list ? my_list_1 = [12, 14, 25, 36, 15] print("The first list is :") print(my_list_1) my_list_2 = [23, 15, 47, 12, 25] print("The second list is :") print(my_list_2) for element in my_list_2: my_list_1.append(element) print("The result is :") print(my_list_1) ...

Read More

Python program to sort matrix based upon sum of rows

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 302 Views

When it is required to sort a matrix based upon the sum of rows, we can use Python's built-in sort() method with a custom key function. The key function calculates the sum of each row, and the matrix is sorted in ascending order based on these sums. Method 1: Using a Custom Function Define a helper function that calculates the sum of a row and use it as the key for sorting ? def sort_sum(row): return sum(row) matrix = [[34, 51], [32, 15, 67], [12, 41], [54, 36, 22]] print("The ...

Read More
Showing 3511–3520 of 25,466 articles
« Prev 1 350 351 352 353 354 2547 Next »
Advertisements