Programming Articles

Page 388 of 2547

Python Program to Determine all Pythagorean Triplets in the Range

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

A Pythagorean triplet is a set of three positive integers a, b, c, such that a² + b² = c². The most famous example is 3, 4, 5 because 3² + 4² = 5² (9 + 16 = 25). This program finds all Pythagorean triplets where the hypotenuse c is within a given range. Understanding Pythagorean Triplets Pythagorean triplets can be generated using Euclid's formula: a = m² - n² b = 2mn c = m² + n² Where m > n > 0, and m and n are coprime integers. Example ...

Read More

Python Program to Print Numbers in a Range (1,upper) Without Using any Loops

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 629 Views

When it is required to print numbers in a given range without using any loop, we can use recursion. This technique involves defining a function that calls itself with a decremented value until it reaches the base case. Below is the demonstration of the same − Example def print_nums(upper_num): if(upper_num > 0): print_nums(upper_num - 1) print(upper_num) upper_lim = 6 print("The upper limit is :") print(upper_lim) print("The numbers are :") print_nums(upper_lim) Output ...

Read More

Convert Nested Tuple to Custom Key Dictionary in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 739 Views

Converting a nested tuple to a custom key dictionary is useful when transforming structured data. We can use list comprehension to iterate through tuples and create dictionaries with custom keys. Syntax [{key1: item[0], key2: item[1], key3: item[2]} for item in nested_tuple] Example Here's how to convert a nested tuple containing employee data into dictionaries with custom keys ? my_tuple = ((6, 'Will', 13), (2, 'Mark', 15), (9, 'Rob', 12)) print("The tuple is:") print(my_tuple) my_result = [{'key': sub[0], 'value': sub[1], 'id': sub[2]} ...

Read More

Flatten tuple of List to tuple in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 407 Views

When working with nested tuple structures, you may need to flatten them into a single level of tuples. This involves recursively processing each element until all nested tuples are converted into individual tuples at the same level. The process uses recursion to traverse through nested structures and identify base cases where tuples contain exactly two non-tuple elements. Recursive Flattening Method Here's how to implement a recursive function to flatten nested tuples ? def flatten_tuple(my_tuple): # Base case: if tuple has 2 elements and first element is not a tuple ...

Read More

Python program to Order Tuples using external List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 401 Views

When you need to reorder tuples based on an external list, you can use dictionary conversion and list comprehension. This technique is useful when you have key-value pairs as tuples and want to sort them according to a specific order defined in another list. Example Here's how to order tuples using an external list ? my_list = [('Mark', 34), ('Will', 91), ('Rob', 23)] print("The list of tuple is:") print(my_list) ordered_list = ['Will', 'Mark', 'Rob'] print("The ordered list is:") print(ordered_list) temp = dict(my_list) my_result = [(key, temp[key]) for key in ordered_list] print("The ordered tuple ...

Read More

Remove Tuples of Length K in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 593 Views

When working with lists of tuples, you may need to remove tuples based on their length. Python provides several approaches to filter out tuples of a specific length K. Using List Comprehension List comprehension offers a concise way to filter tuples by length ? my_list = [(32, 51), (22, 13), (94, 65, 77), (70, ), (80, 61, 13, 17)] print("The list is:") print(my_list) K = 1 print("The value of K is:", K) my_result = [ele for ele in my_list if len(ele) != K] print("The filtered list is:") print(my_result) The ...

Read More

All pair combinations of 2 tuples in Python

Tapas Kumar Ghosh
Tapas Kumar Ghosh
Updated on 25-Mar-2026 426 Views

Python tuples are immutable sequences used to store collections of items. When working with two tuples, you may sometimes need to generate all possible pairs, where each pair contains one element from the first tuple and one from the second. In this article, we will learn different ways to generate all pair combinations from two tuples. Following are input-output scenarios for pairing combinations of two tuples: Input: first_tuple = (1, 3) second_tuple = (7, 9) Output: [(1, 7), (1, 9), (3, 7), (3, 9), (7, 1), (7, 3), (9, 1), (9, 3)] Explanation: Index 0 ...

Read More

Extract digits from Tuple list Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 750 Views

When working with tuple lists in Python, you might need to extract tuples where all elements have a specific number of digits. This can be achieved using list comprehension with the all() function and string length checking. Syntax result = [tuple for tuple in tuple_list if all(len(str(element)) == N for element in tuple)] Example Let's extract tuples where all elements have exactly 2 digits ? my_list = [(67, 2), (34, 65), (212, 23), (17, 67), (18, )] print("The list is:") print(my_list) N = 2 print("The value of N is:") print(N) ...

Read More

Join Tuples if similar initial element in Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 579 Views

When you need to join tuples that have the same initial element, you can use a simple loop to check the first element of each tuple. The extend method helps combine elements from tuples with matching initial values. Example Let's see how to join tuples with similar initial elements ? my_list = [(43, 15), (43, 25), (66, 98), (66, 12), (64, 80)] print("The list is :") print(my_list) my_result = [] for sub in my_list: if my_result and my_result[-1][0] == sub[0]: my_result[-1].extend(sub[1:]) ...

Read More

Closest Pair to Kth index element in Tuple using Python

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 415 Views

When working with tuples, you may need to find the tuple from a list that has the closest value to a reference tuple at a specific index position. Python's enumerate() method combined with abs() function provides an efficient solution. Problem Statement Given a list of tuples and a reference tuple, find which tuple in the list has the value closest to the reference tuple's Kth index element ? Example tuple_list = [(5, 6), (66, 76), (21, 35), (90, 8), (9, 0)] print("The list is:") print(tuple_list) reference_tuple = (17, 23) print("The reference tuple is:") ...

Read More
Showing 3871–3880 of 25,466 articles
« Prev 1 386 387 388 389 390 2547 Next »
Advertisements