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 program to Sort a List of Strings by the Number of Unique Characters
When it is required to sort a list of strings based on the number of unique characters, a method is defined that uses a set operator, the list method and the len method. Example Below is a demonstration of the same − def my_sort_func(my_elem): return len(list(set(my_elem))) my_list = ['python', "Will", "Hi", "how", 'fun', 'learn', 'code'] print("The list is : ") print(my_list) my_list.sort(key = my_sort_func) print("The result is :") print(my_list) Output The list is : ['python', 'Will', 'Hi', 'how', 'fun', 'learn', 'code'] The result is ...
Read MorePython Program that extract words starting with Vowel From A list
When you need to extract words starting with vowels from a list, Python offers several approaches. You can use iteration with the startswith() method, list comprehensions, or the filter() function. Using Loop with startswith() Method This approach uses a flag to check if each word starts with any vowel ? words = ["abc", "phy", "and", "okay", "educate", "learn", "code"] print("The list is:") print(words) result = [] vowels = "aeiou" print("The vowels are:", vowels) for word in words: flag = False for vowel in vowels: ...
Read MorePython program to print Rows where all its Elements' frequency is greater than K
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 MorePython Program to repeat elements at custom indices
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 MorePython – Inverse Dictionary Values List
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 MorePython – Sort String list by K character frequency
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 MorePandas GroupBy – Count the occurrences of each combination
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 MorePython Program to extracts elements from a list with digits in increasing order
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 MorePython – Dual Tuple Alternate summation
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 MorePython – Extract Rear K digits from Numbers
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