Rohan Singh

Rohan Singh

144 Articles Published

Articles by Rohan Singh

Page 5 of 15

How to Run Two Async Functions Forever in Python

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

Async functions, also known as coroutines, are functions that can be paused and resumed during their execution. In Python, the asyncio module provides a powerful framework for writing concurrent code using coroutines. In this article, we will explore how to run two async functions forever using different approaches in Python. Understanding Async Functions Async functions allow for concurrent execution of code without blocking the main thread, enabling efficient utilization of system resources. To define an async function in Python, we use the async keyword before the def statement. Within the async function, we can use the await keyword ...

Read More

How to Return Custom JSON in Django REST Framework

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

Django REST Framework is a powerful toolkit for building APIs in Django. It provides features and functionalities to handle HTTP requests and responses in Python. Django REST Framework uses Serializers and Response classes to return custom JSON data. In this article, we will explore different approaches to return custom JSON in Django REST Framework, along with examples. Using Serializers and Response Class Django REST Framework (DRF) uses serializers to convert complex data types, such as Django models, into JSON, XML, or other content types that can be easily rendered into HTTP responses. To return custom JSON, you can ...

Read More

How to Resample Time Series Data in Python

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

Time series data is a sequence of observations collected over time at regular intervals. This data can be of any domain such as finance, economics, health, and environmental science. The time series data we collect can sometimes be of different frequencies or resolutions, which may not be suitable for our analysis and data modeling process. In such cases, we can resample our time series data by changing the frequencies or resolution through either upsampling or downsampling. Understanding Resampling Resampling involves changing the frequency of time series observations. Upsampling increases frequency (daily to hourly), while downsampling decreases frequency (daily ...

Read More

How to Multiply All Items in a Tuple in Python

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

In Python, tuples are immutable sequences that can contain a collection of elements. We can multiply all the items in a tuple using various methods like using a for loop, using the reduce() function from the functools module, and the math.prod() function. In this article, we will explore all these methods to multiply all items in a tuple in Python. Using for Loop This method is straightforward and easy to understand. It involves iterating over each item in the tuple and multiplying them one by one using a for loop. Syntax for item in my_tuple: ...

Read More

How to Group Strings on Kth character using Python?

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

In Python, grouping strings based on their Kth character is a common string manipulation task. We can accomplish this using several approaches: regular dictionaries, defaultdict from collections, and itertools.groupby(). Each method offers different advantages depending on your specific needs. Method 1: Using a Dictionary The simplest approach uses a regular dictionary to group strings by their Kth character. We manually check if a key exists before adding strings to the corresponding group. Example Here's how to group strings by their 2nd character using a dictionary ? def group_strings_on_kth_char(strings, k): grouped_strings ...

Read More

How to Get the Nth Word in a Given String using Python?

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

Extracting specific words from a string is a common programming task. Python provides several approaches to get the Nth word from a string, including split() method, regular expressions, and custom delimiters. Using split() Method The simplest approach is splitting the string into words and accessing by index ? Syntax words = string.split() nth_word = words[n-1] # n is 1-based index Example def get_nth_word_split(string, n): words = string.split() if 1

Read More

How to Check if two lists are reverse equal using Python?

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

When working with lists in Python, there may be cases where you need to compare whether two lists are reverse equal. This means that the elements in one list are the same as the elements in the other list but in reverse order. Python provides several methods to check if two lists are reverse equal: reversing and comparing lists, using the zip() function, and converting lists to strings. Method 1: Reversing and Comparing Lists The first method involves reversing one of the lists using slicing and then comparing it with the other list. If the reversed list equals ...

Read More

Horizontal Concatenation of Multiline Strings in Python

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

In Python, the concatenation of strings is a common operation that allows you to combine two or more strings into a single string. While concatenating strings vertically (i.e., one below the other) is straightforward, concatenating strings horizontally (i.e., side by side) requires some additional handling, especially when dealing with multiline strings. In this article, we will explore different approaches for performing horizontal concatenation of multiline strings in Python. Using the + Operator The + operator can be used to combine two or more strings into a single string. However, when dealing with multiline strings, using the + operator ...

Read More

Grouping Similar Elements into a Matrix Using Python

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

In data analysis and processing, grouping similar elements into a matrix is essential for better organization and analysis. Python provides several efficient methods to accomplish this task using built-in functions and libraries. In this article, we will explore different approaches to grouping similar elements into a matrix. Using NumPy for Numeric Data NumPy is the most efficient library for working with numeric arrays and matrices. It provides powerful functions to reshape and manipulate data efficiently. Syntax numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0) numpy.reshape(a, newshape, order='C') Example Here we convert elements into a ...

Read More

Group Sublists by another List using Python

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

In Python, we can group sublists by another list using various methods like dictionaries, itertools.groupby() function, and nested list comprehensions. Grouping sublists by another list is useful when analyzing large datasets, categorizing data, text analysis, and natural language processing. This article explores different methods to group sublists by another list in Python. Using a Dictionary A dictionary provides a straightforward approach to group sublists by another list in Python. This method creates groups based on a key and maintains the order specified by the grouping list. Example In this example, we define a function group_sublists that ...

Read More
Showing 41–50 of 144 articles
« Prev 1 3 4 5 6 7 15 Next »
Advertisements