Tapas Kumar Ghosh

Tapas Kumar Ghosh

185 Articles Published

Articles by Tapas Kumar Ghosh

Page 5 of 19

Convert Nested dictionary to Mapped Tuple in Python

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

A nested dictionary is a hierarchical data structure where values themselves are dictionaries. Converting nested dictionaries to mapped tuples means transforming key-value pairs into a list of tuples that preserve the hierarchical relationships. Understanding the Conversion When converting nested dictionaries to mapped tuples, we flatten the structure while maintaining parent-child relationships. For example: # Original nested dictionary nested_dict = { "a": 1, "b": { "c": 2, "d": 3 ...

Read More

Finding the Minimum of Non-Zero Groups using Python

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

Finding the minimum value within non-zero groups is a common data processing task. A non-zero group refers to consecutive elements in a list that are all non-zero, separated by zeros. Python provides several approaches to identify these groups and find their minimum values. Using itertools.groupby() The itertools.groupby() function groups consecutive elements based on a key function. We can use it to separate non-zero elements ? import itertools def minimum_nonzero(data): non_zero_groups = [list(group) for key, group in itertools.groupby(data, key=lambda x: x != 0) if key] min_values = [min(group) ...

Read More

Invoking Function with and without Parenthesis in Python

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

In Python, function invocation refers to calling or executing a function. Understanding the difference between using functions with parentheses () and without parentheses is crucial for proper function handling and references. Key Concepts When you use a function name with parentheses, you invoke (call) the function immediately. When you use a function name without parentheses, you create a reference to the function object without executing it. Syntax Function with parentheses (invokes the function) ? def function_name(): # function body pass function_name() # Calls the ...

Read More

Index Mapping Cypher in Python

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

The Index Mapping Cipher is a technique that extracts characters from a string using digits as indices. Each digit in the index number corresponds to a position in the original string, creating a new string based on these mapped positions. How It Works Given a string and an index number, each digit of the index represents a position in the string ? # Example: "HELLO" with index 1043 # Digit 1 → H[1] = 'E' # Digit 0 → H[0] = 'H' # Digit 4 → H[4] = 'O' # Digit 3 → H[3] ...

Read More

Interconvert Horizontal and Vertical String using Python

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

In Python, we can convert strings between horizontal and vertical formats using built-in functions and loops. This conversion is useful for text formatting, visual creativity, and improving readability in programming applications. Key Functions for String Conversion Essential String Methods Here are the main functions used for horizontal and vertical string interconversion: # str() - Converts value to string text = str(123) print(text) # "123" # replace() - Replaces substrings vertical_text = "ABC" horizontal_text = vertical_text.replace("", "") print(horizontal_text) # "ABC" # split() - Splits string into list words = "Hello World".split(" ") ...

Read More

Finding the First Even Number in a List using Python

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

Finding the first even number in a list is a common programming task in Python. This can be accomplished using various approaches including the next() function, recursion, filter() with lambda, and simple loops. Given a list like [21, 33, 12, 11, 61, 78], the first even number would be 12 since it's the first number divisible by 2. Syntax The key functions used in these examples: next() − Returns the next item from an iterator filter() − Filters elements based on a condition lambda − Creates anonymous functions for short operations Using next() ...

Read More

Find the Common Keys from two Dictionaries in Python

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

Dictionaries are key-value data structures in Python where each key is unique. Finding common keys between two dictionaries is a frequent task in data processing and comparison operations. Let's explore different methods to find common keys with this example ? Input: dict1 = {'I': 10, 'II': 20, 'III': 30, 'IV': 40} dict2 = {'II': 40, 'V': 60, 'VI': 80, 'I': 90} Expected Output: {'I', 'II'} Using Set Intersection with keys() The most efficient approach uses set intersection to find common keys ? dict1 = {'A': 20, 'T': 30, 'W': 40, ...

Read More

Convert Matrix to Custom Tuple Matrix in Python

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

In Python, a tuple is an immutable data structure represented by parentheses (). Converting a matrix to a custom tuple matrix means transforming each element in the matrix into a single-element tuple. This creates a matrix where each value becomes (value, ) instead of just value. For example, converting [[1, 2], [3, 4]] results in [[(1, ), (2, )], [(3, ), (4, )]]. Using List Comprehension List comprehension provides a concise way to transform matrix elements ? def matrix_to_custom_tuple(matrix): return [[(value, ) for value in row] for row in matrix] ...

Read More

How to Set up Multiple Subplots with Group Legends using Plotly in Python

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

Multiple subplots allow you to display several independent plots within a single figure for easy comparison. Plotly provides powerful tools to create subplots with shared legends, making data visualization more organized and informative. Key Functions make_subplots() Creates a grid layout for multiple plots ? from plotly.subplots import make_subplots fig = make_subplots(rows=2, cols=2, subplot_titles=("Plot 1", "Plot 2", "Plot 3", "Plot 4")) add_trace() Adds individual plot traces to specific subplot positions ? import plotly.graph_objects as go fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6], name="Data Series"), row=1, col=1) Basic Multiple ...

Read More

Filter Non-None dictionary keys in Python

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

Filtering Non-None dictionary keys in Python is a common task when working with data that contains missing or empty values. Python provides several efficient methods to remove None values from dictionaries using built-in functions like items(), filter(), and dictionary comprehensions. Let's explore different approaches to transform a dictionary like {'A': 11, 'B': None, 'C': 29, 'D': None} into {'A': 11, 'C': 29}. Using Dictionary Comprehension Dictionary comprehension provides the most readable and Pythonic way to filter None values ? def filter_none(dictionary): return {key: value for key, value in dictionary.items() if value ...

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