Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 424 of 2547
Python | Remove empty tuples from a list
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 MorePython Program to Edit objects inside tuple
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 MorePython Program to Find Longest Common Substring using Dynamic Programming with Bottom-Up Approach
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 MorePython - Assign a specific value to Non Max-Min elements in Tuple
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 MorePython program to sort a list of tuples by second Item
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 MorePython Program to Print an Identity Matrix
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 MorePython program to search an element in a doubly linked list
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 MorePython program to rotate doubly linked list by N nodes
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 MorePython program to remove duplicate elements from a Doubly Linked List
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 MorePython program to insert a new node at the middle of the Doubly Linked List
When you need to insert a new node in the middle of a doubly linked list, you create a Node class with three attributes: the data, access to the next node, and access to the previous node. This allows bidirectional traversal and insertion at any position. Node Class Structure The Node class defines the structure of each element in the doubly linked list ? class Node: def __init__(self, my_data): self.previous = None self.data = my_data ...
Read More