Articles on Trending Technologies

Technical articles with clear explanations and examples

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 277 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 279 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 331 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 336 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 355 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

Python – Sort by Maximum digit in Element

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 305 Views

When it is required to sort by maximum digit in element, a method is defined that uses str() and max() method to determine the result. This technique converts each number to a string, finds its largest digit, and uses that digit as the sorting key. Syntax def max_digits(element): return max(str(element)) list.sort(key=max_digits) Example Here's how to sort a list of numbers by their maximum digit ? def max_digits(element): return max(str(element)) my_list = [224, 192, 145, 18, 3721] print("The list is :") print(my_list) ...

Read More

Python – Calculate the percentage of positive elements of the list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 515 Views

When calculating the percentage of positive elements in a list, we can use list comprehension along with the len() method to count positive values and compute the percentage. Example Here's how to calculate the percentage of positive elements in a list ? my_list = [14, 62, -22, 13, -87, 0, -21, 81, 29, 31] print("The list is :") print(my_list) my_result = (len([element for element in my_list if element > 0]) / len(my_list)) * 100 print("The result is :") print(my_result) Output The list is : [14, 62, -22, 13, -87, ...

Read More

Python – Filter Rows with Range Elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 358 Views

When working with nested lists, you may need to filter rows that contain all elements from a specific range. Python provides an elegant solution using list comprehension with the all() function to check if every element in a range exists within each row. Syntax filtered_rows = [row for row in nested_list if all(element in row for element in range(start, end + 1))] Example Let's filter rows that contain all numbers from 2 to 5 ? my_list = [[3, 2, 4, 5, 10], [32, 12, 4, 51, 10], [12, 53, 11], [2, 3, ...

Read More

Python program to Sort Strings by Punctuation count

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 368 Views

When you need to sort strings by their punctuation count, you can define a custom function that counts punctuation marks and use it as a sorting key. This technique uses Python's string.punctuation module and list comprehension to efficiently count punctuation characters. Example Here's how to sort strings based on the number of punctuation marks they contain − from string import punctuation def get_punctuation_count(my_str): return len([element for element in my_str if element in punctuation]) my_list = ["python@%^", "is", "fun!", "to@#r", "@#$learn!"] print("The list is :") print(my_list) my_list.sort(key = get_punctuation_count) ...

Read More
Showing 3921–3930 of 61,299 articles
« Prev 1 391 392 393 394 395 6130 Next »
Advertisements