Tapas Kumar Ghosh

Tapas Kumar Ghosh

185 Articles Published

Articles by Tapas Kumar Ghosh

185 articles

Python - K difference index Pairing in List

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 254 Views

K difference index pairing creates pairs of elements from a list where each element is paired with another element that is exactly K positions ahead. For example, with K=2, element at index 0 pairs with element at index 2, index 1 with index 3, and so on. Given the list ["A", "B", "C", "D", "E", "F"] and K=2, the result would be ["AC", "BD", "CE", "DF"]. Using map() with operator.concat The map() function applies operator.concat to corresponding elements from two sliced lists ? import operator # Initialize list my_list = ["T", "U", "T", "O", ...

Read More

Check if the Sum of Digits of a Number N Divides It

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 411 Views

A number is divisible by the sum of its digits when the remainder of dividing the number by its digit sum equals zero. For example, if N = 36, the digit sum is 3 + 6 = 9, and since 36 % 9 = 0, the number 36 is divisible by its digit sum. Algorithm The algorithm follows these steps ? Extract each digit of the number using modulo operator (% 10) Add all digits to get the sum Check if the original number is divisible by this sum using modulo operator Return true if remainder ...

Read More

How to Sort a Pandas DataFrame by Date?

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 3K+ Views

Sorting a Pandas DataFrame by date is a common operation in data analysis. Pandas provides several methods to accomplish this, with sort_values() being the most efficient. Before sorting, ensure your date column is in proper datetime format using to_datetime(). Basic Date Sorting with sort_values() The most straightforward method is using sort_values() after converting string dates to datetime format ? import pandas as pd # Create sample DataFrame with date strings data = { 'Date': ['2023-06-26', '2023-06-24', '2023-06-28', '2023-06-25'], 'Sales': [100, 200, 300, 150] } df ...

Read More

Python - Filter Tuples with All Even Elements

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 380 Views

Filtering tuples with all even elements is a common operation in Python data processing. This involves checking if every element in each tuple is divisible by 2 and keeping only those tuples that satisfy this condition. Using List Comprehension with all() The most Pythonic approach combines list comprehension with the all() function to filter tuples where every element is even ? tuple_list = [(6, 4, 2, 8), (5, 6, 7, 6), (8, 0, 2), (7, ), (2, 4, 6)] result = [tup for tup in tuple_list if all(ele % 2 == 0 for ele in ...

Read More

Check if a Word is Present in a Sentence

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 1K+ Views

In Python, checking if a word is present in a sentence is a common task in text processing and string manipulation. Python provides multiple approaches including the in operator, find() method, and split() method for word-by-word matching. Using the in Operator The simplest way to check if a word exists in a sentence is using Python's in operator ? sentence = "The bestseller book is amazing" word = "book" if word in sentence: print("The given word is present in the sentence.") else: print("The given word is not ...

Read More

Check if a number with even number of digits is palindrome or not

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 466 Views

A palindrome is a number that reads the same forwards and backwards. This article shows how to check if a number with an even number of digits is a palindrome in Python. Examples include 2662, 4224, 44, 1001, etc. Using String Comparison The simplest approach converts the number to a string and compares it with its reverse ? def is_even_digit_palindrome(num): # Convert number to string num_str = str(num) # Check if even number of digits ...

Read More

Python - K List Dictionary Mesh

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 226 Views

The term dictionary mesh refers to creating a nested dictionary structure where each element from the first list becomes a key, and each element from the second list becomes a nested key with empty lists as values. This creates a mesh-like structure that can hold organized data. Understanding Dictionary Mesh Given two input lists ? list1 = [10, 20, 30] list2 = [40, 50] The dictionary mesh creates this structure ? {10: {40: [], 50: []}, 20: {40: [], 50: []}, 30: {40: [], 50: []}} Method 1: Using Nested ...

Read More

How to iterate over Columns in Numpy

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 2K+ Views

NumPy provides several methods to iterate over columns in a 2D array. The most common approaches include using nditer() with transpose, array transpose directly, apply_along_axis(), and manual iteration with indexing. Syntax Here are the key functions used for column iteration ? np.nditer(array.T) # Iterator with transpose array.T # Array transpose np.apply_along_axis() # Apply function along axis array.shape[1] ...

Read More

Python - K Elements Reversed Slice

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 307 Views

K Elements Reversed Slice extracts the last K elements from a list and returns them in reverse order. This technique is useful for data analysis, queue processing, and extracting recent entries from datasets. What is K Elements Reversed Slice? A K elements reversed slice takes the last K elements from a list and reverses their order. For example, if we have [1, 2, 3, 4, 5] and K=3, we get the last 3 elements [3, 4, 5] reversed as [5, 4, 3]. Using Slicing The most concise approach uses Python's slice notation with negative indexing and ...

Read More

Python - Filter unequal elements of two lists corresponding same index

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 27-Mar-2026 655 Views

When working with two lists in Python, you may need to find elements that are equal at the same index positions. This is useful for data comparison, synchronization, and filtering operations. For example, given two lists: list1 = [10, 20, 30, 40, 50] list2 = [1, 20, 3, 40, 50] # Elements at same index: 20, 40, 50 are equal Using zip() and List Comprehension The most Pythonic approach uses zip() to pair elements and list comprehension to filter matches − list1 = [1, 2, 3, 4, 5] list2 = [1, 2, ...

Read More
Showing 1–10 of 185 articles
« Prev 1 2 3 4 5 19 Next »
Advertisements