Programming Articles

Page 424 of 2547

Python - Replace duplicate Occurrence in String

Akshitha Mote
Akshitha Mote
Updated on 25-Mar-2026 942 Views

In this article, we will explore different ways to replace duplicate occurrences in a string. Before understanding the solution let's try to understand the problem statement with an example. Consider a string "Ram lives in Hyderabad. Ram is studying in class 10. Ram is the class Monitor of the class." In this string "Ram" appears multiple times we need to replace the duplicate occurrences with 'He', keeping the first occurrence unchanged. Methods to Replace Duplicate Occurrences Following are the multiple ways to replace duplicate occurrences in a string − Using List comprehension Using for loop ...

Read More

Python | Remove empty tuples from a list

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 1K+ Views

When it is required to remove empty tuples from a list of tuples, Python provides several approaches. An empty tuple () evaluates to False in boolean context, making it easy to filter out. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). A list of tuples basically contains tuples enclosed in a list. Using List Comprehension The most pythonic way is to use list comprehension with a boolean check − def remove_empty(my_tuple_list): my_tuple_list = [t for t ...

Read More

Python Program to Edit objects inside tuple

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 227 Views

When it is required to edit the objects inside a tuple, simple indexing can be used. While tuples are immutable, they can contain mutable objects like lists that can be modified in-place. A tuple can store heterogeneous values (i.e data of any data type like integer, floating point, strings, lists, and so on). When a tuple contains mutable objects, those objects can be modified without changing the tuple's identity. Example Below is a demonstration of editing a list inside a tuple − my_tuple = (45, 67, [35, 66, 74], 89, 100) print("The tuple is :") ...

Read More

Python Program to Find Longest Common Substring using Dynamic Programming with Bottom-Up Approach

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 415 Views

Finding the longest common substring using dynamic programming with a bottom-up approach involves building a solution by solving smaller subproblems first. This method uses a 2D table to store intermediate results, avoiding redundant calculations and ensuring optimal performance. The algorithm works by comparing characters from both strings and building up the solution from the bottom of the problem space ? Algorithm Overview The bottom-up approach creates a 2D table where val[i][j] represents the length of the common substring ending at position i in the first string and position j in the second string. We fill this table ...

Read More

Python - Assign a specific value to Non Max-Min elements in Tuple

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 193 Views

When working with tuples, you may need to replace all elements except the maximum and minimum values with a specific value. Python provides the max() and min() methods to find extreme values, which can be used with conditional logic to transform tuple elements. The max() method returns the largest element in an iterable, while min() returns the smallest. The tuple() method converts any iterable into a tuple. Example Here's how to replace non max-min elements with a specific value ? my_tuple = (25, 56, 78, 91, 23, 11, 0, 99, 32, 10) print("The tuple is:") ...

Read More

Python program to sort a list of tuples by second Item

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 734 Views

When it is required to sort a list of tuples based on the second item, the lambda function and sorted() method can be used. A list can be used to store heterogeneous values (i.e data of any data type like integer, floating point, strings, and so on). A list of tuples basically contains tuples enclosed in a list. Anonymous function is a function which is defined without a name. In general, functions in Python are defined using def keyword, but anonymous function is defined with the help of lambda keyword. It takes a single expression, but can take ...

Read More

Python Program to Print an Identity Matrix

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 912 Views

An identity matrix is a square matrix where the diagonal elements are 1 and all other elements are 0. In Python, you can create an identity matrix using nested loops or the NumPy library. Using Nested Loops The basic approach uses two nested loops to check if the row index equals the column index ? n = 4 print("The value of n has been initialized to " + str(n)) for i in range(0, n): for j in range(0, n): if(i == j): ...

Read More

Python program to search an element in a doubly linked list

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 663 Views

When searching for an element in a doubly linked list, we need to create a Node class with three attributes: the data, a pointer to the next node, and a pointer to the previous node. We also need a main class to manage the list operations including adding nodes, displaying nodes, and searching for specific elements. In a doubly linked list, each node has bidirectional pointers − one pointing to the next node and another to the previous node. This allows traversal in both directions, making it more flexible than a singly linked list. Node Class Structure ...

Read More

Python program to rotate doubly linked list by N nodes

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 347 Views

When working with doubly linked lists, rotation involves moving the first N nodes from the beginning to the end of the list. This requires a Node class with three attributes: data, next pointer, and previous pointer for bidirectional traversal. Node Class Structure Each node contains data and pointers to both next and previous nodes ? class Node: def __init__(self, my_data): self.previous = None self.data = my_data self.next = ...

Read More

Python program to remove duplicate elements from a Doubly Linked List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 362 Views

When it is required to remove duplicate elements in a doubly linked list, a Node class needs to be created. In this class, there are three attributes: the data that is present in the node, the access to the next node, and the access to the previous node of the linked list. Below is a demonstration for the same − Node Class Structure The Node class represents each element in the doubly linked list − class Node: def __init__(self, my_data): self.previous = None ...

Read More
Showing 4231–4240 of 25,466 articles
« Prev 1 422 423 424 425 426 2547 Next »
Advertisements