Programming Articles - Page 1335 of 3366

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

Filter tuples according to list element presence in Python

AmitDiwan
Updated on 12-Mar-2021 05:43:40

459 Views

When it is required to filter tuples based on the list element that is present, 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.Below is a demonstration of the same −ExampleLive Demomy_list = [(11, 14), (54, 56, 87), (98, 0, 10), (13, 76)] target_list = [34, 11] print("The list is : ") print(my_list) my_result ... Read More

Summation of tuples in list in Python

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

2K+ Views

When it is required to sum the elements present in a list of tuple, the 'map' method and the 'sum' 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 'sum' method can be used to add the elements in an iterable.Below is a demonstration of the same −ExampleLive Demomy_list = ... Read More

Flatten Tuples List to String in Python

AmitDiwan
Updated on 12-Mar-2021 05:41:26

378 Views

When it is required to flatten a list of tuples into a string format, the 'str' method and the 'strip' 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. The 'strip' method will remove the specific character/value. The 'str' method will convert the given data type into a string data type.Below is a demonstration of the same −ExampleLive Demomy_list = [(11, 14), (54, 56), (98, 0), (13, 76)] print("The list is : ... Read More

How to Concatenate tuples to nested tuples in Python

AmitDiwan
Updated on 12-Mar-2021 05:40:44

1K+ Views

When it is required to concatenate tuples to nested tuples, 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 is used for addition or concatenation operation.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = ( 7, 8, 3, 4, 3, 2), my_tuple_2 = (9, 6, 8, 2, 1, 4), print ("The first tuple is : " ) print(my_tuple_1) ... Read More

Convert tuple to float value in Python

AmitDiwan
Updated on 12-Mar-2021 05:40:22

4K+ Views

When it is required to convert a tuple to a float value, the 'join' method, 'float' method, 'str' method and generator expression can be used.Generator is a simple way of creating iterators. It automatically implements a class with '__iter__()' and '__next__()' methods and keeps track of the internal states, as well as raises 'StopIteration' exception when no values are present that could be returned.The 'float' method converts a given element to float data type.The 'str' method converts the given element into string data type.Below is a demonstration of the same −ExampleLive Demomy_tuple_1 = ( 7, 8) print ("The first ... Read More

Find maximum of minimum for every window size in a given array in C++

sudhir sharma
Updated on 12-Mar-2021 05:49:21

975 Views

In this problem, we are given an array arr[] of size n. Our task is to Find the maximum of the minimum for every window size in a given array.Problem Description − We need to find the maximum of the minimum of a window size that varies from 1 to n. For this we will consider subarrays of the given window size, find the minimum element of each subarray and then find the maximum of all the minimums.Let’s take an example to understand the problem, Inputarr[] = {4, 1, 2, 4, 5, 1, 2, 4}Output5 4 2 1 1 1 ... Read More

Advertisements