Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 348 of 2547
Python – Sort by Units Digit in a List
When sorting a list by its units digit (last digit), we can use a custom key function with the sort() method. This function converts each number to a string and extracts the last character using negative indexing. Using a Custom Key Function The most straightforward approach is to define a helper function that extracts the units digit ? def unit_sort(element): return str(element)[-1] numbers = [716, 134, 343, 24742] print("The list is:") print(numbers) numbers.sort(key=unit_sort) print("The result is:") print(numbers) The list is: [716, 134, 343, 24742] The ...
Read MorePython – Remove rows with Numbers
When working with nested lists in Python, you may need to remove rows that contain numeric values. This can be achieved using list comprehension combined with the not, any, and isinstance functions. Example Below is a demonstration of removing rows containing numbers ? data_rows = [[14, 'Pyt', 'fun'], ['Pyt', 'is', 'best'], [23, 51], ['Pyt', 'fun']] print("The list is :") print(data_rows) result = [row for row in data_rows if not any(isinstance(element, int) for element in row)] print("The result is :") print(result) Output The list is : [[14, 'Pyt', 'fun'], ['Pyt', ...
Read MorePython - Sort Matrix by Maximum Row element
When you need to sort a matrix by the maximum element in each row, you can use Python's sort() method with a custom key function. This technique is useful for organizing data based on the highest value in each row. Using a Custom Key Function Define a function that returns the maximum element of each row, then use it as the sorting key ? def sort_max(row): return max(row) my_list = [[15, 27, 18], [39, 20, 13], [13, 15, 56], [43, 13, 25]] print("The original matrix is:") print(my_list) my_list.sort(key=sort_max, reverse=True) ...
Read MorePython – Display the key of list value with maximum range
When it is required to display the key of list value with maximum range, a simple iteration is used. The range of a list is the difference between its maximum and minimum values. Example Below is a demonstration of the same − my_dict = {"pyt" : [26, 12, 34, 21], "fun" : [41, 27, 43, 53, 18], "learning" : [21, 30, 29, 13]} print("The dictionary is :") print(my_dict) max_range = 0 result_key = "" for key, values in my_dict.items(): current_range = max(values) - min(values) if ...
Read MorePython – Find occurrences for each value of a particular key
When working with a list of dictionaries, you may need to find how many times each value appears for a particular key. Python provides several approaches to count occurrences of values for a specific key. Using collections.Counter The most straightforward approach is using Counter to count occurrences ? from collections import Counter my_dict = [ {'pyt': 13, 'fun': 44}, {'pyt': 63, 'best': 15}, {'pyt': 24, 'fun': 34}, {'pyt': 47, 'best': 64}, {'pyt': 13, ...
Read MorePython program to extract Mono-digit elements
When it is required to extract mono-digit elements (numbers where all digits are the same), list comprehension and the all operator are used. What are Mono-digit Elements? Mono-digit elements are numbers where all digits are identical, such as 1, 22, 333, 4444, etc. For single-digit numbers, they are naturally mono-digit. Example my_list = [863, 1, 463, "pyt", 782, 241, "is", 639, 4, "fun", 22, 333] print("The list is :") print(my_list) my_result = [item for item in my_list if isinstance(item, int) and all(str(digit) == str(item)[0] for digit in str(item))] print("The result is :") ...
Read MorePython – Sort Dictionaries by Size
When working with Python dictionaries, you often need to sort them by their size (number of key-value pairs). Python provides several approaches to accomplish this using the len() function as a sorting key. Using a Custom Function with sort() The most straightforward approach is to define a helper function that returns the length of each dictionary ? def get_len(element): return len(element) dict_list = [ {24: 56, 29: 11, 10: 22, 42: 28}, {54: 73, 59: 11}, {13: ...
Read MorePython – Cross Pattern Pairs in List
When it is required to display cross pattern pairs in list, a list comprehension and the multiplication operator are used to create all possible products between elements of two lists. What is Cross Pattern Pairs? Cross pattern pairs refer to multiplying each element from the first list with every element from the second list, creating a Cartesian product of multiplications. Example my_list_1 = [14, 35, 26] my_list_2 = [36, 24, 12] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) result = [i * j for j in my_list_1 for ...
Read MorePython – Nearest occurrence between two elements in a List
When you need to find the nearest occurrence between two elements in a list, you can create a function that calculates the minimum distance between all occurrences of the first element and the target element. This approach uses list comprehension and the abs() function to find the closest match. Example Here's how to find the index of the nearest occurrence ? def nearest_occurrence_list(my_list, x, y): if x not in my_list or y not in my_list: return -1 ...
Read MorePython – Filter Tuples with Integers
When it is required to filter tuples that contain only integers, a simple iteration with the 'not' operator and the 'isinstance' method can be used to check each element's data type. Example Below is a demonstration of filtering tuples with integers − my_tuple = [(14, 25, "Python"), (5, 6), (3, ), ("cool", )] print("The tuple is :") print(my_tuple) my_result = [] for sub in my_tuple: temp = True for element in sub: if not isinstance(element, int): ...
Read More