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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Python – Check if particular value is present corresponding to K key
When working with a list of dictionaries in Python, you often need to check if a particular value exists for a specific key across all dictionaries. This can be efficiently done using list comprehension with the in operator. Example Here's how to check if a value is present corresponding to a specific key ? my_list = [ {"python": "14", "is": "great", "fun": "1"}, {"python": "cool", "is": "fun", "best": "81"}, {"python": "93", "is": "CS", "amazing": "16"} ] print("The list is:") print(my_list) K = ...
Read MoreHow to find the common elements in a Pandas DataFrame?
Finding common elements between Pandas DataFrames is essential for data analysis tasks like identifying shared records or performing data validation. Python provides several methods including merge(), intersection(), and set operations. Using merge() Method The merge() method performs an inner join by default, returning only common rows between DataFrames − import pandas as pd df1 = pd.DataFrame({ "x": [5, 2, 7, 0], "y": [4, 7, 5, 1], "z": [9, 3, 5, 1] }) df2 = pd.DataFrame({ "x": [5, 2, ...
Read MorePython – Sort by Uppercase Frequency
Sorting strings by uppercase frequency means arranging them based on the count of capital letters they contain. This is useful for organizing text data by capitalization patterns. Python's sort() method with a custom key function makes this straightforward. How It Works The approach uses a custom function that counts uppercase letters in each string, then sorts the list using this count as the sorting key ? def count_uppercase(text): return len([char for char in text if char.isupper()]) words = ["pyt", "is", "FUN", "to", "Learn"] print("Original list:") print(words) words.sort(key=count_uppercase) print("Sorted ...
Read MorePython program to extract only the numbers from a list which have some specific digits
When you need to extract numbers from a list that contain only specific digits, you can use list comprehension with the all() function. This technique filters numbers by checking if every digit in each number exists in your allowed digits set. Syntax result = [num for num in numbers if all(int(digit) in allowed_digits for digit in str(num))] Example Let's extract numbers that contain only the digits 2, 3, 4, and 5 ? numbers = [3345, 2345, 1698, 2475, 1932] print("The original list is:") print(numbers) allowed_digits = [2, 3, 4, 5] ...
Read MorePython program to Sort a List of Dictionaries by the Sum of their Values
When working with lists of dictionaries, you may need to sort them based on the sum of their values. Python provides multiple approaches to achieve this using the sort() method with custom key functions. Method 1: Using a Custom Function Define a function to calculate the sum of dictionary values and use it as the sorting key − def sum_value(row): return sum(list(row.values())) my_dict = [{21: 13, 44: 35, 34: 56}, {11: 75, 70: 19, 39: 70}, {1: 155}, {48: 29, 17: 53}] print("The dictionary is:") print(my_dict) my_dict.sort(key=sum_value) print("The ...
Read MorePython – Remove Dictionaries with Matching Values
When working with lists of dictionaries, you may need to remove dictionaries that have matching values for a specific key. Python provides several approaches using dictionary comprehensions and set operations. Basic Example Here's how to remove dictionaries from one list that have matching values with dictionaries in another list ? # List of dictionaries to filter dict_list_1 = [ {'Hi': 32, "there": 32, "Will": 19}, {'Hi': 19, "there": 100, "Will": 13}, {'Hi': 72, "there": 19, "Will": 72} ] print("The first dictionary list ...
Read MorePython – Find the frequency of numbers greater than each element in a list
When it is required to find the frequency of numbers greater than each element in a list, a list comprehension and the 'sum' method can be used to count elements efficiently. Example The following example demonstrates how to count elements greater than each position in the list ? my_list = [24, 13, 72, 22, 12, 47] print("The list is :") print(my_list) my_result = [sum(1 for element in my_list if element > index) for index in my_list] print("The result is :") print(my_result) Output The list is : [24, 13, 72, ...
Read MorePython Program to test whether the length of rows are in increasing order
When it is required to test whether the length of rows are in increasing order, a simple iteration and a Boolean value is used. This technique helps verify that each nested list has more elements than the previous one. Example my_list = [[55], [12, 17], [25, 32, 24], [58, 36, 57, 19, 14]] print("The list is :") print(my_list) my_result = True for index in range(len(my_list) - 1): if len(my_list[index + 1])
Read MorePython program to find the sum of all even and odd digits of an integer list
When it is required to find the sum of all even and odd digits of an integer list, a simple iteration and the modulus operator are used. Below is a demonstration of the same − Example my_list = [369, 793, 2848, 4314, 57467] print("The list is :") print(my_list) sum_odd = 0 sum_even = 0 for index in my_list: for element in str(index): if int(element) % 2 == 0: ...
Read MorePython – Test for Word construction from character list
When it is required to test if a word can be constructed from a character list, the all() function and the count() method are used together. This approach checks if each character in the target word appears enough times in the available character list. Syntax all(word.count(char)
Read More