AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 122 of 840

Python – Test if Rows have Similar frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 217 Views

When checking if rows have similar frequency in a list of lists, you can use Python's Counter class along with the all() function to compare element frequencies across all rows. Example Here's how to test if all rows contain the same elements with identical frequencies ? from collections import Counter my_list = [[21, 92, 64, 11, 3], [21, 3, 11, 92, 64], [64, 92, 21, 3, 11]] print("The list is :") print(my_list) my_result = all(dict(Counter(row)) == dict(Counter(my_list[0])) for row in my_list) if my_result == True: print("All rows have ...

Read More

Python Program that filters out non-empty rows of a matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 271 Views

When working with matrices (lists of lists), you often need to filter out empty rows. Python provides several approaches to remove non-empty rows from a matrix using list comprehension, the filter() function, or loops. Using List Comprehension List comprehension with len() provides a clean and readable solution ? my_matrix = [[21, 52, 4, 74], [], [7, 8, 4, 1], [], [9, 2]] print("The original matrix is:") print(my_matrix) # Filter out empty rows using list comprehension filtered_matrix = [row for row in my_matrix if len(row) > 0] print("The matrix after filtering empty rows:") print(filtered_matrix) ...

Read More

Python – Test if all elements are unique in columns of a Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 536 Views

When it is required to test if all elements are unique in columns of a matrix, a simple iteration and a list comprehension along with the 'set' operator are used. Below is a demonstration of the same − Example my_matrix = [[11, 24, 84], [24, 55, 11], [7, 11, 9]] print("The matrix is:") print(my_matrix) my_result = True for index in range(len(my_matrix[0])): column = [row[index] for row in my_matrix] if len(list(set(column))) != len(column): ...

Read More

Python – Remove characters greater than K

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 249 Views

When working with strings, you may need to filter out characters based on their position in the alphabet. This technique removes characters that appear after a certain position K in the alphabet using the ord() function to get Unicode values. Understanding the Approach The method compares each character's alphabetical position with K. Since lowercase 'a' has ASCII value 97, we subtract 97 from each character's ASCII value to get its alphabetical position (a=0, b=1, c=2, etc.). Example Here's how to remove characters greater than K from a list of strings ? words = ["python", ...

Read More

Python – Check if any list element is present in Tuple

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

When it is required to check if any list element is present in a tuple or not, Python provides several approaches. We can use simple iteration, the any() function, or set intersection for efficient checking. Using Simple Iteration This approach uses a loop to check each list element against the tuple ? my_tuple = (14, 35, 27, 99, 23, 89, 11) print("The tuple is :") print(my_tuple) my_list = [16, 27, 88, 99] print("The list is :") print(my_list) my_result = False for element in my_list: if element in my_tuple: ...

Read More

Python Program to sort rows of a matrix by custom element count

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 268 Views

When sorting rows of a matrix based on custom element count, we can define a helper function that counts how many elements from each row match our custom criteria. This approach uses list comprehension with the len() method to determine the sort order. Example Let's sort matrix rows by counting how many elements from each row appear in our custom list ? def get_count_matrix(my_key): return len([element for element in my_key if element in custom_list]) my_list = [[31, 5, 22, 7], [85, 5], [9, 11, 22], [7, 48]] print("The list is ...

Read More

Python Program to Filter Rows with a specific Pair Sum

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 261 Views

When working with lists of numbers, we often need to filter rows that contain a specific pair sum. This involves checking if any two elements in each row add up to a target value and keeping only those rows that satisfy this condition. Problem Overview Given a list of lists (rows) and a target sum, we want to filter and keep only the rows where at least one pair of elements adds up to the target value ? Method: Using Helper Function with List Comprehension We'll create a helper function to check for pair sums and ...

Read More

Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 964 Views

When working with lists of tuples, you often need to sort them based on the last element of each tuple. Python provides multiple approaches: using sorted() with a key function, the list.sort() method, or implementing a custom bubble sort algorithm. Using sorted() with Key Function The most Pythonic approach uses sorted() with a lambda function as the key ? data = [(1, 92), (34, 25), (67, 89)] print("Original tuple list:") print(data) # Sort by last element of each tuple sorted_data = sorted(data, key=lambda x: x[-1]) print("Sorted list of tuples:") print(sorted_data) Original tuple ...

Read More

Python Program to Find the Cumulative Sum of a List where the ith Element is the Sum of the First i+1 Elements From The Original List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 508 Views

When it is required to find the cumulative sum of a list where the ith element is the sum of the first i+1 elements from the original list, we can use different approaches. The cumulative sum creates a new list where each element represents the running total up to that position. Method 1: Using List Comprehension This approach uses list comprehension with slicing to calculate cumulative sums ? def cumulative_sum(my_list): cumulative_list = [] my_length = len(my_list) cumulative_list = [sum(my_list[0:x:1]) for x in range(1, my_length+1)] ...

Read More

Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10

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

When it is required to find all numbers in a range that are perfect squares and the sum of digits in the number is less than 10, list comprehension is used. A perfect square is a number that can be expressed as the product of an integer with itself. Below is the demonstration of the same − Example lower_limit = 5 upper_limit = 50 numbers = [] numbers = [x for x in range(lower_limit, upper_limit + 1) if (int(x**0.5))**2 == x and sum(list(map(int, str(x)))) < 10] print("The result is:") print(numbers) Output The ...

Read More
Showing 1211–1220 of 8,392 articles
« Prev 1 120 121 122 123 124 840 Next »
Advertisements