AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 98 of 840

Python - Filter dictionaries by values in Kth Key in list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 312 Views

When working with a list of dictionaries, you might need to filter them based on whether a specific key's value appears in a given list. This can be accomplished using simple iteration or more Pythonic approaches like list comprehension. Using For Loop Method The basic approach iterates through each dictionary and checks if the target key's value exists in the search list ? data_list = [{"Python": 2, "is": 4, "cool": 11}, {"Python": 5, "is": 1, "cool": 1}, ...

Read More

Python - Most common Combination in Matrix

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 331 Views

When it is required to find the most common combination in a matrix, a simple iteration, along with the sort() method and Counter method is used. Example Below is a demonstration of the same − from collections import Counter from itertools import combinations my_list = [[31, 25, 77, 82], [96, 15, 23, 32]] print("The list is :") print(my_list) my_result = Counter() for elem in my_list: if len(elem) < 2: continue elem.sort() ...

Read More

How to Groupby values count on the Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 598 Views

To perform groupby value counts in Pandas, use the groupby(), size(), and unstack() methods. This technique helps you count occurrences of grouped data and reshape the results into a cross-tabulation format. Creating the DataFrame First, let's create a sample DataFrame with product information ? import pandas as pd # Create a DataFrame with 3 columns dataFrame = pd.DataFrame({ 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'], 'Quantity': [10, 50, 10, 20, 25, ...

Read More

Python - Unique values count of each Key

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 457 Views

When it is required to find unique values count of every key, we can use several approaches including iteration with the 'append' method, the set() function, or dictionary-based counting methods. Using Iteration with Append Method This approach manually tracks unique values by checking if each element already exists in a filtered list ? my_list = [12, 33, 33, 54, 84, 16, 16, 16, 58] print("The list is :") print(my_list) filtered_list = [] elem_count = 0 for item in my_list: if item not in filtered_list: ...

Read More

Python program to Mark duplicate elements in string

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 286 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
AmitDiwan
Updated on 26-Mar-2026 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
AmitDiwan
Updated on 26-Mar-2026 321 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
AmitDiwan
Updated on 26-Mar-2026 270 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
AmitDiwan
Updated on 26-Mar-2026 549 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
AmitDiwan
Updated on 26-Mar-2026 274 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]]

Read More
Showing 971–980 of 8,392 articles
« Prev 1 96 97 98 99 100 840 Next »
Advertisements