Articles on Trending Technologies

Technical articles with clear explanations and examples

Python - Group Similar Value List to Dictionary

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 2K+ Views

In Python, we can group similar value lists to dictionaries using methods like for loops with conditional statements, defaultdict, and groupby from the itertools module. Grouping similar values together is useful when analyzing complex data and organizing elements by their occurrences. Method 1: Using For Loop and Conditional Statements The simplest method to group similar values uses a for loop and conditional statements. We initialize an empty dictionary, iterate over each item, and check if it already exists as a key ? Syntax groups[key].append(element) The append() method adds an element to the end ...

Read More

Python - Group Similar Keys in Dictionary

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 2K+ Views

In Python, similar keys in a dictionary can be grouped using various methods such as defaultdict, dictionary of lists, and the itertools module with groupby() function. During data analysis, we often need to group similar keys together based on certain criteria. Method 1: Using defaultdict Python's defaultdict class from the collections module provides a convenient way to group similar keys. It automatically initializes a default value when a new key is accessed. Syntax from collections import defaultdict groups = defaultdict(list) groups[key].append(item) Here, defaultdict(list) creates a dictionary that automatically creates an empty list for ...

Read More

Python - Group list of Tuples to Dictionary

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 751 Views

In Python, we can group list of tuples to a dictionary using different methods like using a for loop and conditional statements, using the defaultdict class from the collections module, and using the groupby() function from the itertools module. In this article we will explore all these methods and implement them to group a list of tuples to dictionary. Method 1: Using For Loop and Conditional Statements This method involves using a for loop to iterate over the list of tuples. We check if the key exists in the dictionary and add the corresponding tuple as a value ...

Read More

Python - Group first elements by second elements in Tuple list

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 917 Views

In Python, grouping first elements by second elements in a tuple list means organizing tuples that share the same second element into groups. For example, if we have tuples like ('Apple', 'Fruit') and ('Banana', 'Fruit'), we want to group ['Apple', 'Banana'] under the key 'Fruit'. Using a Dictionary This approach uses a dictionary where the second element becomes the key, and first elements are stored as values in a list ? # Sample tuple list data = [('Apple', 'Fruit'), ('Banana', 'Fruit'), ('Carrot', 'Vegetable'), ('Potato', 'Vegetable')] # Grouping elements using a dictionary grouped_data = {} for ...

Read More

Python - Group Concatenate Till K

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 191 Views

Group concatenate till K means joining elements within a sequence until encountering a specific delimiter value K. Python provides several approaches including loops, itertools.groupby(), and regular expressions. This technique is useful for splitting data into meaningful chunks based on separator values. Method 1: Using a Loop and Accumulator This approach iterates through the list, accumulating elements until the delimiter K is found. When K is encountered, accumulated elements are joined and added to results. Syntax ''.join(sequence) list_name.append(element) The join() method concatenates sequence elements into a string, while append() adds elements to a list. ...

Read More

Python - Golomb Encoding for b=2n and b!=2n

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 513 Views

Golomb encoding is a data compression technique used to encode non-negative integers with specific distributions. It was introduced by Solomon W. Golomb in 1966 and has applications in video compression, image compression, and data storage. This article explores Golomb encoding for two cases: when the base is a power of 2 (b=2^n) and when it's not (b≠2^n). Golomb Encoding for b=2^n (Base is Power of 2) When the base is a power of 2, Golomb encoding becomes simpler. Let's see how it works with b = 4 (2^2). Steps for Power of 2 Base Step 1: ...

Read More

Python - Get the object with the max attribute value in a list of objects

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 4K+ Views

In Python, we can get the object with a max attribute value in a list of objects using the max() function with a lambda function, using the operator module, or using a custom comparison function. In this article, we will explore different methods to find objects with the maximum attribute value. Algorithm To get the object with the maximum attribute value in a list of objects, you can follow this common algorithm: Initialize a variable, let's call it max_object, to None initially. Iterate over each object in the list. ...

Read More

Python - Get the indices of all occurrences of an element in a list

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 12K+ Views

To retrieve the indices of all occurrences of a specific element in a list, Python provides several efficient methods. In this article, we'll explore four different approaches: using a for loop, list comprehension, the enumerate() function, and the index() method with a while loop. Using for loop The most straightforward approach is to iterate through the list using indices and compare each element with the target value. Syntax for i in range(len(data)): if data[i] == element: indices.append(i) Example def ...

Read More

Python - Get sum of last K list items using slice

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 820 Views

In Python, the slice method allows us to extract specific elements from a sequence, such as strings, lists, or tuples. It provides a concise and flexible way to work with subsequences within a larger sequence. In this article, we will explore how to obtain the sum of the last K items in a list using slice operations. Algorithm To find the sum of the last K items in a list, we can follow a simple algorithm: Accept the list and the value of K as inputs. Use the slice operator to extract the last K items ...

Read More

Python - Get particular Nested level Items from Dictionary

Rohan Singh
Rohan Singh
Updated on 27-Mar-2026 2K+ Views

Dictionaries in Python allow you to store key-value pairs, making it easy to organize and access data efficiently. Sometimes, we may need to retrieve specific items from nested levels within a dictionary. We can use recursion, isinstance() with recursion, and dict.get() methods to extract nested-level items from dictionaries. In this article, we will explore different ways to get particular nested-level items from Dictionary in Python. Nested Dictionary A nested dictionary is a dictionary that contains other dictionaries as values. This allows for the creation of hierarchical structures, where data is organized in a tree-like fashion. Each level of ...

Read More
Showing 1–10 of 61,303 articles
« Prev 1 2 3 4 5 6131 Next »
Advertisements