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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Python Count set bits in a range?
A given positive number when converted to binary has a number of set bits. Set bits in a binary number are represented by 1. In this article we will see how to count the number of set bits in a specific range of positions within a binary representation of a number. Using bin() and String Slicing In the below example we take a number and apply the bin() function to get the binary value. Then we slice it to remove the prefixes and count set bits in the specified range ? Example def SetBits_cnt(n, l, ...
Read MorePython Convert nested dictionary into flattened dictionary?
As the world embraces more unstructured data, we come across many formats of data where the data structure can be deeply nested like nested JSONs. Python has the ability to deal with nested data structure by concatenating the inner keys with outer keys to flatten the data. In this article we will take a nested dictionary and flatten it. Using a Recursive Approach In this approach we design a function to recursively process each item in the dictionary. We pass the dictionary, design a place holder for the output dictionary, the key and separator as parameters. We use ...
Read MorePython Check if suffix matches with any string in given list?
Checking if a suffix matches with any string in a list is a common string manipulation task in Python. A suffix is the ending part of a string, and we need to verify if any string in our list ends with the given suffix pattern. Understanding Suffixes A suffix is a substring that appears at the end of a string. For example, in "Sunday", the suffix "day" appears at the end. Let's see how to check if any string in a list ends with a specific suffix. Using any() with endswith() The most efficient approach combines ...
Read MoreMemory-mapped file support in Python (mmap)?
Python's mmap module provides memory-mapped file support, allowing you to map files directly into memory for efficient reading, writing, and searching. Instead of making system calls like read() and write(), memory mapping loads file data into RAM where you can manipulate it directly. Basic Memory Mapped File Reading Memory mapping loads the entire file into memory as a file-like object. You can then slice and access specific portions efficiently ? import mmap # Create a sample file first with open('sample.txt', 'w') as f: f.write('The emissions from gaseous compounds are harmful to ...
Read MoreHyperText Markup Language support in Python?
Python has the capability to process HTML files through the HTMLParser class in the html.parser module. It can detect the nature of HTML tags, their position, and many other properties. It has functions which can also identify and fetch the data present in an HTML file. The HTMLParser class allows you to create custom parser classes that can process only the tags and data that you define. You can handle start tags, end tags, and text data between tags. Basic HTML File Structure Let's start with a simple HTML file that we'll parse ? ...
Read MorePython Get the real time currency exchange rate?
Python is excellent at handling API calls for retrieving real-time and historical currency exchange rates. This article demonstrates two primary methods: using the forex-python module and making direct API calls. Using forex-python Module The forex-python module provides the most direct way to get currency conversion rates. It offers simple functions that accept currency codes and return conversion rates. Real-time Exchange Rates Here's how to get live currency conversion rates ? from forex_python.converter import CurrencyRates c = CurrencyRates() # Get USD to GBP conversion rate rate = c.get_rate('USD', 'GBP') print(f"1 USD = {rate} ...
Read MorePython - Combine two lists by maintaining duplicates in first list
In data analysis using Python, we often need to combine two lists while handling duplicate elements carefully. This article shows how to combine two lists by maintaining all elements from the first list (including duplicates) and adding only unique elements from the second list. Using extend() with Generator Expression This approach preserves the original first list and uses a generator expression to filter unique elements from the second list ? # Given lists first_list = ['A', 'B', 'B', 'X'] second_list = ['B', 'X', 'Z', 'P'] # Create result list starting with first list result = ...
Read MorePython - Convert 1D list to 2D list of variable length
A list in Python is normally a 1D list where the elements are listed one after another. But in a 2D list we have lists nested inside the outer list. In this article we will see how to create a 2D list from a given 1D list with variable-length sublists based on specified lengths. Using Slicing with Index Tracking In this approach we will create a for loop to iterate through each required length and use slicing to extract the appropriate elements from the 1D list. We keep track of the current index position and increment it by ...
Read MoreHow to Change the Thickness of hr Tag using CSS?
The tag is used to draw horizontal lines on a web page. This tag is one of the most useful HTML tags for separating content by drawing a horizontal line between different sections. In this guide, we will learn how to change the thickness of an tag using CSS with different methods. Syntax hr { height: value; border: value; } The hr Tag in HTML The tag stands for horizontal rule. It is a self-closing tag that creates a visual divider between sections ...
Read MorePython - Convert a list of lists into tree-like dict
Given a nested list, we want to convert it to a dictionary whose elements represent a tree data structure. In this article, we will explore two approaches to transform a list of lists into a hierarchical dictionary structure. Using Simple Iteration and Slicing We reverse the items in each list using slicing and then build the tree structure by checking and adding keys at each level ? Example def create_tree(nested_list): new_tree = {} for list_item in nested_list: current_tree = ...
Read More