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
Server Side Programming Articles
Page 97 of 2109
Python - Group list of Tuples to Dictionary
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 MorePython - Group first elements by second elements in Tuple list
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 MorePython - Group Concatenate Till K
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 MorePython - Golomb Encoding for b=2n and b!=2n
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 MorePython - Get the object with the max attribute value in a list of objects
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 MorePython - Get the indices of all occurrences of an element in a list
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 MorePython - Get sum of last K list items using slice
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 MorePython - Get particular Nested level Items from Dictionary
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 MoreHyperlink-Induced Topic Search (HITS) Algorithm using Networxx Module - Python
The Hyperlink-Induced Topic Search (HITS) algorithm is a popular algorithm used for web link analysis, particularly in search engine ranking and information retrieval. HITS identifies authoritative web pages by analyzing the links between them. In this article, we will explore how to implement the HITS algorithm using the NetworkX module in Python. Understanding HITS Algorithm The HITS algorithm is based on the idea that authoritative web pages are often linked to by other authoritative pages. It works by assigning two scores to each web page ? Authority Score: Measures the quality and relevance of information provided ...
Read MoreHow to Sort a Dictionary by Kth Index Value in Python?
In Python, we can sort a dictionary by the value of a specific index (kth index value) using various approaches like the sorted() function with lambda, itemgetter() from the operator module, or custom functions. Let's explore these methods with practical examples. Method 1: Using sorted() with Lambda Function This method uses the sorted() function with a lambda function to specify the sorting criterion based on the kth index value. Syntax sorted_dict = dict(sorted(dictionary.items(), key=lambda x: x[1][k])) Example Here we sort a dictionary by the second element (index 1) of each list value ...
Read More