Pranavnath

Pranavnath

389 Articles Published

Articles by Pranavnath

Page 2 of 39

Print diagonals of 2D list in Python

Pranavnath
Pranavnath
Updated on 27-Mar-2026 1K+ Views

Python provides powerful features for working with two-dimensional data structures. One common task is extracting diagonal elements from a 2D list (matrix). This article demonstrates how to print both major and minor diagonals using simple iteration methods. Understanding Matrix Diagonals Before extracting diagonals, let's understand the concept with a visual representation: ...

Read More

Python - Print alphabets till N

Pranavnath
Pranavnath
Updated on 27-Mar-2026 784 Views

Python provides several ways to print alphabets up to a specified position N. When N is 5, for example, the program prints the first 5 letters: a, b, c, d, e. This is useful for creating patterns, educational programs, and understanding ASCII character manipulation. Understanding the Problem In the English alphabet, there are 26 letters total. When we specify N=5, we want to print the first 5 letters (a through e). The position N determines how many alphabets to display from the beginning. Using the String Module The string module provides predefined constants like ascii_lowercase and ...

Read More

Python – Product of squares in list

Pranavnath
Pranavnath
Updated on 27-Mar-2026 263 Views

The product of squares in a list means calculating the square of each element first, then multiplying all the squared values together. Python provides multiple approaches to solve this problem efficiently. Understanding the Problem Given a list like [9, -4, 8, -1], we need to ? Square each element: 9² = 81, (-4)² = 16, 8² = 64, (-1)² = 1 Multiply all squares: 81 × 16 × 64 × 1 = 82944 Method 1: Using Iteration Iterate through each element, square it, and multiply with the running product ? # ...

Read More

Python – Product of prefix in list

Pranavnath
Pranavnath
Updated on 27-Mar-2026 406 Views

Finding the product of prefix in a list means calculating cumulative products where each position contains the product of all elements from the start up to that position. Python offers several approaches to accomplish this task efficiently. What is Product of Prefix? Given a list, the product of prefix transforms it so that each element becomes the product of all preceding elements including itself. For example: Original List [5, 6, 7, 8] Product of Prefix [5, 30, 210, 1680] The calculation works as follows: Position 0: 5 = ...

Read More

Python – Prefix sum Subarray till False value

Pranavnath
Pranavnath
Updated on 27-Mar-2026 206 Views

In Python, a prefix sum subarray till false value means computing cumulative sums of elements until encountering a "falsy" value (like 0, False, None, empty list, etc.). This technique is useful for conditional cumulative calculations where processing should stop at certain conditions. Understanding Prefix Sum with False Values Consider an array where we want to calculate prefix sums but stop when we encounter a false value ? Index 0 1 2 3 4 5 6 Value 2 10 4 3 0 10 -2 Prefix Sum 2 12 16 19 Stop - ...

Read More

Python – Priority key assignment in dictionary

Pranavnath
Pranavnath
Updated on 27-Mar-2026 435 Views

Priority key assignment in Python dictionaries allows you to process dictionary elements in a specific order based on their importance. This technique is particularly useful when working with data that has different priority levels. What is Priority Key Assignment? Priority key assignment means defining which dictionary keys should be processed first based on their importance. Instead of processing keys randomly, you can establish a priority order to ensure critical data is handled before less important data. Basic Dictionary Syntax # Basic dictionary structure sample_dict = {'hello': 'all', 'welcome': 897} print(sample_dict) {'hello': 'all', ...

Read More

Python – Print list after removing element at given index

Pranavnath
Pranavnath
Updated on 27-Mar-2026 652 Views

Python lists are mutable data structures that allow you to store elements of different data types. Sometimes you need to remove elements at specific positions. This article demonstrates three methods to print a list after removing elements at given indices. Original List: 0 1 2 3 4 ...

Read More

Python – Product of kth column in list of lists

Pranavnath
Pranavnath
Updated on 27-Mar-2026 187 Views

Python lists can contain sublists, creating a two-dimensional structure. Sometimes you need to calculate the product of elements in a specific column across all rows. This article demonstrates how to find the product of the kth column in a list of lists using different approaches. For example, consider this 3x3 matrix: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # To get product of column 2 (index 2): 3 * 6 * ...

Read More

Remove all sublists outside a given range using Python

Pranavnath
Pranavnath
Updated on 27-Mar-2026 351 Views

In Python, you can remove sublists that fall outside a given range using several approaches. This article explores three effective methods: list comprehension, iterative removal, and filtering with append. Each method offers different advantages depending on your specific needs. Using List Comprehension List comprehension provides a concise way to filter sublists based on range conditions. This method creates a new list containing only sublists where all elements fall within the specified range. Algorithm Step 1 − Define a function that takes the main list and range bounds as parameters Step 2 − Use list comprehension ...

Read More

How to Remove all digits from a list of strings using Python?

Pranavnath
Pranavnath
Updated on 27-Mar-2026 563 Views

When working with lists of strings in Python, you might need to remove all numeric digits from each string. This is a common data cleaning task in text processing. Python provides several approaches to accomplish this: using string methods, regular expressions, or character filtering. Using replace() Method The simplest approach is to use the replace() method to remove each digit individually ? def remove_digits(string_list): return [s.replace('0', '').replace('1', '').replace('2', '').replace('3', '').replace('4', '').replace('5', '').replace('6', '').replace('7', '').replace('8', '').replace('9', '') for s in string_list] # List of strings containing digits data = ['John53mass', '66elsa98Marvel', '300perfect04stay'] ...

Read More
Showing 11–20 of 389 articles
« Prev 1 2 3 4 5 39 Next »
Advertisements