Python program to Mark duplicate elements in string

AmitDiwan
Updated on 26-Mar-2026 02:09:08

296 Views

When it is required to mark duplicate elements in a string, list comprehension along with the count method is used. This technique helps identify repeated elements by appending occurrence numbers to duplicates. Example Below is a demonstration of the same − my_list = ["python", "is", "fun", "python", "is", "fun", "python", "fun"] print("The list is :") print(my_list) my_result = [value + str(my_list[:index].count(value) + 1) if my_list.count(value) > 1 else value for index, value in enumerate(my_list)] print("The result is :") print(my_result) Output The list is : ['python', 'is', 'fun', 'python', 'is', ... Read More

Python - Calculate the count of column values of a Pandas DataFrame

AmitDiwan
Updated on 26-Mar-2026 02:08:54

1K+ Views

To calculate the count of column values in a Pandas DataFrame, use the count() method. This method returns the number of non-null values in each column, making it useful for data validation and analysis. Importing Required Library First, import the Pandas library − import pandas as pd Counting Values in a Specific Column You can count non-null values in a specific column by accessing the column and applying count() − import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { ... Read More

Python - Index Directory of Elements

AmitDiwan
Updated on 26-Mar-2026 02:08:35

328 Views

When it is required to index directory of elements in a list, list comprehension along with set operator is used. This creates a dictionary mapping each unique element to all its index positions in the list. Syntax {key: [index for index, value in enumerate(list) if value == key] for key in set(list)} Example Below is a demonstration of creating an index directory ? my_list = [81, 36, 42, 57, 68, 12, 26, 26, 38] print("The list is :") print(my_list) my_result = {key: [index for index, value in enumerate(my_list) ... Read More

Python - Convert List to custom overlapping nested list

AmitDiwan
Updated on 26-Mar-2026 02:08:18

280 Views

When it is required to convert a list to a customized overlapping nested list, an iteration along with the 'append' method can be used. This technique creates sublists of a specified size with a defined step interval, allowing elements to overlap between consecutive sublists. Syntax for index in range(0, len(list), step): result.append(list[index: index + size]) Parameters The overlapping nested list conversion uses two key parameters: step − The interval between starting positions of consecutive sublists size − The length of each sublist Example Below is ... Read More

Python Program to get indices of sign change in a list

AmitDiwan
Updated on 26-Mar-2026 02:07:57

554 Views

When it is required to get indices of sign change in a list, a simple iteration along with append method can be used. A sign change occurs when consecutive numbers have different signs (positive to negative or negative to positive). Example Below is a demonstration of finding sign change indices ? my_list = [71, 24, -34, -25, -76, 87, 29, -60, 70, 8] print("The list is :") print(my_list) my_result = [] for index in range(0, len(my_list) - 1): # Check for sign change between current and next element ... Read More

Python - Restrict Tuples by frequency of first element's value

AmitDiwan
Updated on 26-Mar-2026 02:07:44

276 Views

When working with lists of tuples, you may need to restrict tuples based on how frequently their first element appears. This technique is useful for filtering duplicates or limiting occurrences to a specific threshold. Example Below is a demonstration of restricting tuples by frequency of the first element ? my_list = [(21, 24), (13, 42), (11, 23), (32, 43), (25, 56), (73, 84), (91, 40), (40, 83), (13, 27)] print("The list is :") print(my_list) my_key = 1 my_result = [] mems = dict() for sub in my_list: if sub[0] not in mems.keys(): mems[sub[0]] = 1 else: mems[sub[0]] += 1 if mems[sub[0]]

Python - Find starting index of all Nested Lists

AmitDiwan
Updated on 26-Mar-2026 02:07:27

475 Views

When working with nested lists, you might need to find the starting index of each sublist if all elements were flattened into a single list. This is useful for indexing and data processing tasks. Understanding the Problem Given a nested list like [[51], [91, 22, 36, 44], [25, 25]], if we flatten it to [51, 91, 22, 36, 44, 25, 25], we want to know where each original sublist starts: [0, 1, 5]. Method 1: Using Simple Iteration Track the cumulative length as we iterate through each sublist ? my_list = [[51], [91, 22, ... Read More

Python - Check if list contain particular digits

AmitDiwan
Updated on 26-Mar-2026 02:07:07

457 Views

When you need to check if all numbers in a list are composed only of specific digits, you can use string operations and set comparison. This is useful for validating numbers that should only contain certain digits. Example Below is a demonstration of checking if list elements contain only particular digits − numbers = [415, 133, 145, 451, 154] print("The list is:") print(numbers) allowed_digits = [1, 4, 5, 3] # Convert allowed digits to string set for faster lookup digit_string = ''.join([str(digit) for digit in allowed_digits]) all_numbers = ''.join([str(num) for num in numbers]) ... Read More

Python - Total equal pairs in List

AmitDiwan
Updated on 26-Mar-2026 02:06:49

717 Views

When it is required to find the total equal pairs in a list, we can use the set() function along with the floor division operator // and iteration. This approach counts how many pairs can be formed from duplicate elements in the list. Example Below is a demonstration of finding equal pairs in a list − numbers = [34, 56, 12, 32, 78, 99, 67, 34, 52, 78, 99, 10, 0, 11, 23, 9] print("The list is :") print(numbers) unique_elements = set(numbers) total_pairs = 0 for element in unique_elements: total_pairs ... Read More

Python program to return rows that have element at a specified index

AmitDiwan
Updated on 26-Mar-2026 02:06:35

269 Views

When it is required to return rows that have an element at a specified index, a simple iteration and the append() function can be used. This technique is useful for filtering rows based on matching elements at specific positions. Basic Example Below is a demonstration of comparing elements at a specific index ? my_list_1 = [[21, 81, 35], [91, 14, 0], [64, 61, 42]] my_list_2 = [[21, 92, 63], [80, 19, 65], [54, 65, 36]] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_key = 0 my_result = [] ... Read More

Advertisements