Python – Extract Strings with Successive Alphabets in Alphabetical Order

AmitDiwan
Updated on 26-Mar-2026 01:10:56

269 Views

When working with strings, you may need to extract strings that contain successive alphabets in alphabetical order. This means finding strings where at least two consecutive characters follow each other in the alphabet (like 'hi' where 'h' and 'i' are consecutive). Understanding Successive Alphabets Successive alphabets are characters that follow each other in the alphabet sequence. For example: 'ab' - 'a' and 'b' are successive 'hi' - 'h' and 'i' are successive 'xyz' - 'x', 'y', 'z' are all successive Method: Using ord() Function The ord() function returns the Unicode code point of a ... Read More

Python – Test for desired String Lengths

AmitDiwan
Updated on 26-Mar-2026 01:10:41

243 Views

When it is required to test for desired string lengths, a simple iteration and the len() method can be used. This technique helps verify that each string in a list matches its corresponding expected length. Using Basic Iteration Below is a demonstration of checking string lengths using a for loop ? strings = ["python", "is", "fun", "to", "learn", "Will", "how"] print("The list is :") print(strings) expected_lengths = [6, 2, 3, 2, 5, 4, 3] result = True for index in range(len(strings)): if len(strings[index]) != expected_lengths[index]: ... Read More

Python program to Sort a List of Strings by the Number of Unique Characters

AmitDiwan
Updated on 26-Mar-2026 01:10:20

387 Views

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 More

Python Program that extract words starting with Vowel From A list

AmitDiwan
Updated on 26-Mar-2026 01:10:03

2K+ Views

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 More

Python program to print Rows where all its Elements' frequency is greater than K

AmitDiwan
Updated on 26-Mar-2026 01:09:47

251 Views

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 More

Python Program to repeat elements at custom indices

AmitDiwan
Updated on 26-Mar-2026 01:09:29

177 Views

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 More

Python – Inverse Dictionary Values List

AmitDiwan
Updated on 26-Mar-2026 01:09:10

451 Views

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 More

Python – Sort String list by K character frequency

AmitDiwan
Updated on 26-Mar-2026 01:08:50

445 Views

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 More

Pandas GroupBy – Count the occurrences of each combination

AmitDiwan
Updated on 26-Mar-2026 01:08:31

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
Updated on 26-Mar-2026 01:08:06

231 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

Advertisements