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
Articles on Trending Technologies
Technical articles with clear explanations and examples
Find maximum path length in a binary matrix in Python
In this problem, we are given a square matrix of size m × n with each element either 0 or 1. If an element has value 1, it represents a connected cell; if the value is 0, it represents a disconnected cell. Our task is to find the maximum path length in a binary matrix by converting at most one 0 to 1. Problem Description To solve this problem, we need to find the largest length path consisting of all connected cells (1s) in the matrix. We can convert at most one 0 to 1 to maximize the ...
Read MoreRepeating tuples N times in Python
When it is required to repeat a tuple N times, the * operator can be used. A tuple is an immutable data type that ensures read-only access to its elements. Python provides multiple ways to repeat tuples, each serving different purposes. Basic Tuple Repetition The * operator behaves like a multiplication operator for sequences ? my_tuple = (11, 14, 0) print("Original tuple:", my_tuple) N = 3 repeated_tuple = my_tuple * N print(f"Repeated {N} times:", repeated_tuple) Original tuple: (11, 14, 0) Repeated 3 times: (11, 14, 0, 11, 14, 0, 11, 14, 0) ...
Read MoreTest if tuple is distinct in Python
When it is required to test if a tuple has distinct elements in it, the set method and the len method can be used. Python comes with a datatype known as set. This set contains elements that are unique only. The len method gives the length of the parameter passed to it. Method 1: Using set() and len() Convert the tuple to a set and compare lengths. If they are equal, all elements are distinct ? my_tuple = (11, 14, 54, 0, 58, 41) print("The tuple is:") print(my_tuple) is_distinct = len(set(my_tuple)) == len(my_tuple) ...
Read MoreCheck if two list of tuples are identical in Python
In Python, you may need to check if two lists of tuples are identical. A list of tuples is a data structure where each element is a tuple containing multiple values. Python provides several methods to compare such structures. Using the == Operator The most straightforward way to check if two lists of tuples are identical is using the == operator. This compares both the content and order of elements ? list_1 = [(11, 14), (54, 58)] list_2 = [(98, 0), (10, 13)] print("First list:", list_1) print("Second list:", list_2) result = list_1 == list_2 ...
Read MoreAddition of tuples in Python
In Python, tuples are used to store an immutable sequence of elements. In this article, we will learn different methods to implement a Python program to add corresponding elements of tuples. Here, you are given two equally sized tuples in Python and your task is to create a new tuple containing sum of corresponding elements of the tuples. Consider the following example scenario − Scenario # Input Tuples: tup1 = (3, 6, 9, 45, 6) tup2 = (11, 14, 21, 0, 6) # Expected Output: (14, 20, 30, 45, 12) # The corresponding elements ...
Read MoreFilter tuples according to list element presence in Python
When filtering tuples based on whether they contain specific elements from a target list, list comprehension combined with the any() function provides an efficient solution. This technique is useful for data filtering operations where you need to find tuples that share common elements with a reference list. A list of tuples contains multiple tuples as elements, and list comprehension offers a concise way to iterate and apply conditions to filter the data. Basic Filtering Example Here's how to filter tuples that contain any element from a target list ? my_list = [(11, 14), (54, 56, ...
Read MoreSummation of tuples in list in Python
When working with a list of tuples, you often need to calculate the total sum of all elements across all tuples. Python provides several approaches to achieve this using map(), sum(), and other methods. A list of tuples contains tuples enclosed in a list, where each tuple can hold multiple numeric values. The map() function applies a given operation to every item in an iterable, while sum() adds all elements in an iterable. Using map() and sum() The most concise approach combines map() and sum() to first sum each tuple, then sum all results ? ...
Read MoreFlatten Tuples List to String in Python
When you need to flatten a list of tuples into a string format, Python provides several approaches using built-in methods like str(), strip(), and join(). A list of tuples contains multiple tuples enclosed within square brackets. The str() method converts any data type to string format, while strip() removes specified characters from the beginning and end of a string. Using str() and strip() The simplest approach converts the entire list to a string and removes the outer brackets ? my_list = [(11, 14), (54, 56), (98, 0), (13, 76)] print("The list is:") print(my_list) ...
Read MoreConvert tuple to float value in Python
Converting a tuple to a float value in Python can be useful when you have numeric values stored separately that need to be combined into a decimal number. This can be achieved using the join() method with a generator expression and the float() function. A generator expression is a concise way to create iterators that automatically implements __iter__() and __next__() methods. The str() method converts elements to string format, while float() converts the final result to float data type. Basic Conversion Example Here's how to convert a tuple containing two integers into a float value ? ...
Read MoreFind Dissimilar Elements in Tuples in Python
When it is required to find dissimilar elements in tuples, the set operator and the ^ operator can be used. Python comes with a datatype known as set. This set contains elements that are unique only. The set is useful in performing operations such as intersection, difference, union and symmetric difference. The ^ operator performs the symmetric difference operation on sets. It returns elements that are in either set but not in both sets. Example Below is a demonstration of finding dissimilar elements between two tuples ? my_tuple_1 = ((7, 8), (3, 4), (3, ...
Read More