Found 26504 Articles for Server Side Programming

Remove similar element rows in tuple Matrix in Python

AmitDiwan
Updated on 12-Mar-2021 05:53:28

135 Views

When it is required to remove similar element rows in a tuple matrix, the list comprehension and the 'all' method can be used.The list comprehension is a shorthand to iterate through the list and perform operations on it.The 'all' method checks to see if all the values inside an iterable are True values. If yes, it returns True, else returns False.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = ((11, 14, 0), (78, 33, 11), (10, 78, 0), (78, 78, 78)) print("The tuple of tuples is : ") print(my_tuple_1) my_result = tuple(ele for ele in my_tuple_1 if ... Read More

Conversion to N*N tuple matrix in Python

AmitDiwan
Updated on 12-Mar-2021 05:51:59

427 Views

When it is required to convert an N*N tuple into a matrix, a simple loop and the * operator can be used.The * operator can be used to get the product of two values. It can also be used to multiple a single value multiple times and display it on the console.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = ((11, 14), (0, 78), (33, 11), (10, 78)) print("The tuple of tuple is : ") print(my_tuple_1) N = 4 print("The value of N has been initialized to "+ str(N)) my_result = [] for tup in my_tuple_1 : ... Read More

Removing duplicates from tuple in Python

AmitDiwan
Updated on 12-Mar-2021 05:50:17

1K+ Views

When it is required to remove duplicates from a tuple, the list comprehension is 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 tuple basically contains tuples enclosed in a list.The list comprehension is a shorthand to iterate through the list and perform operations on it.Below is a demonstration of the same −ExampleLive Demomy_list_1 = [(11, 14), (0, 78), (33, 11), (0, 78)] print("The list of tuple is : ") print(my_list_1) my_unique_list = list(set([i for i in my_list_1])) print("The list of ... Read More

Record Similar tuple occurrences in Python

AmitDiwan
Updated on 12-Mar-2021 05:49:25

106 Views

When it is required to record similar tuple occurrences, the 'map' method, the 'Counter' method and the '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 tuple basically contains tuples enclosed in a list.The map function applies a given function/operation to every item in an iterable (such as list, tuple). It returns a list as the result.The 'sorted' method is used to sort the elements of a list.The 'Counter' is a sub-class that helps count hashable objects, i.e it ... Read More

Find Maximum difference between tuple pairs in Python

AmitDiwan
Updated on 12-Mar-2021 05:48:49

319 Views

When it is required to find the maximum difference between tuple pairs, the 'max' method and the list comprehension 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 tuple basically contains tuples enclosed in a list.The list comprehension is a shorthand to iterate through the list and perform operations on it.The 'max' method returns the maximum of values by taking an iterable as argument.Below is a demonstration of the same −ExampleLive Demomy_list_1 = [(11, 14), (0, 78), (33, 67), (89, 0)] ... Read More

Find maximum path length in a binary matrix in Python

sudhir sharma
Updated on 12-Mar-2021 05:53:41

297 Views

In this problem, we are given a square matrix mat[][] of size m X n with each element either 0 or 1. If an element has value 1, this means it is connected, if the value is 0, this means it is not-connected. Our task is to find the maximum path length in a binary matrix.Problem Description − To solve the problem, we need to find the largest length path on the matrix, which means all 1 elements in the matrix. Before finding the path, we will convert at-most one 0 to 1.Let’s take an example to understand the problem, ... Read More

Repeating tuples N times in Python

AmitDiwan
Updated on 12-Mar-2021 05:47:05

3K+ Views

When it is required to repeat a tuple 'N' times, the '*' operator can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access.The '*' operator behaves like a multiplication operator.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = (11, 14, 0) print("The tuple is : ") print(my_tuple_1) N = 5 my_result = ((my_tuple_1, ) * N) print("The tuple after duplicating "+ str(N) ... Read More

Test if tuple is distinct in Python

AmitDiwan
Updated on 12-Mar-2021 05:46:36

1K+ Views

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.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = (11, 14, 54, 0, 58, 41) print("The tuple is : ") print(my_tuple_1) my_result = len(set(my_tuple_1)) == len(my_tuple_1) print("Is the tuple distinct ? ") print(my_result)OutputThe tuple is : (11, 14, 54, 0, 58, 41) Is the tuple distinct ? ... Read More

Check if two list of tuples are identical in Python

Sindhura Repala
Updated on 14-Apr-2025 14:19:12

3K+ Views

Lists of Tuples in Python A tuple is a built-in Python data structure for storing multiple items in a single variable. While both tuples and lists can organize ordered collections of items, tuples are immutable, whereas lists are mutable. A list can store heterogeneous values, including various types of data such as integers, floating-point numbers, strings, and more. A list of tuples refers to tuples encapsulated within a list. Comparing Lists of Tuples The '==' operator is used to compare two iterables and determine if they are equal. It is used to check whether two lists are equal. Therefore, we ... Read More

Addition of tuples in Python

Farhan Muhamed
Updated on 14-Jul-2025 18:54:28

3K+ Views

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) Output: (14, 20, 30, 45, 12 ) Explanation: The corresponding elements that are added to get ... Read More

Advertisements