Sort by each tuple’s maximum

Python Practice › Tuple Programs › Program 60

Sort by each tuple's maximum

EasyTuple Programs · 60 of 70

Write a Python program to sort tuples by their maximum value, largest first.

Input
[(1, 5, 7), (3, 4, 2), (4, 9, 0)]
Output
[(4, 9, 0), (1, 5, 7), (3, 4, 2)]

Approach

  1. key=max with reverse=True.

Solution

Python
print(sorted([(1, 5, 7), (3, 4, 2), (4, 9, 0)], key=max, reverse=True))
Output
[(4, 9, 0), (1, 5, 7), (3, 4, 2)]

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Remove nested tuples

Python Practice › Tuple Programs › Program 59

Remove nested tuples

EasyTuple Programs · 59 of 70

Write a Python program to remove nested tuples from a tuple.

Input
("s", "q", "a", ("t", "o", "o", "l", "s"), "i", "s", "b", "e", "s", "t")
Output
('s', 'q', 'a', 'i', 's', 'b', 'e', 's', 't')

Approach

  1. Keep items that are not tuples.

Solution

Python
t = ("s", "q", "a", ("t", "o", "o", "l", "s"), "i", "s", "b", "e", "s", "t")print(tuple(x for x in t if not isinstance(x, tuple)))
Output
('s', 'q', 'a', 'i', 's', 'b', 'e', 's', 't')

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Empty set when nothing is symmetrical

Python Practice › Tuple Programs › Program 58

Empty set when nothing is symmetrical

MediumTuple Programs · 58 of 70

Write a Python program that returns an empty set if no tuples in a list are symmetrical.

Input
[(1, 5, 7), (3, 4), (4, 9, 0)]
Output
set()
{(1, 2)}

Approach

  1. Same check as before; the set stays empty when nothing matches.

Solution

Python
def symmetrical(data):    pool = set(data)    return {t for t in data if t[::-1] in pool and t[::-1] != t and t < t[::-1]} print(symmetrical([(1, 5, 7), (3, 4), (4, 9, 0)]))print(symmetrical([(1, 2), (2, 1)]))
Output
set()
{(1, 2)}

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Symmetrical tuples

Python Practice › Tuple Programs › Program 57

Symmetrical tuples

MediumTuple Programs · 57 of 70

Write a Python program to extract the tuples whose reverse is also in the list.

Input
[("a", "b", "c"), ("d", "e"), ("c", "b", "a")]
Output
{('a', 'b', 'c')}

Approach

  1. Put all tuples in a set and keep those whose reverse is in it.

Solution

Python
data = [("a", "b", "c"), ("d", "e"), ("c", "b", "a")]pool = set(data)result = {t for t in data if t[::-1] in pool and t[::-1] != t and t < t[::-1]}print(result)
Output
{('a', 'b', 'c')}

Line by line

Line 3

t < t[::-1] keeps only one tuple from each symmetrical pair.

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

List of lists to tuple of tuples

Python Practice › Tuple Programs › Program 56

List of lists to tuple of tuples

EasyTuple Programs · 56 of 70

Write a Python program to convert a list of lists into a tuple of tuples.

Input
[["sqatools"], ["is"], ["best"]]
Output
(('sqatools',), ('is',), ('best',))

Approach

  1. tuple(tuple(x) for x in data).

Solution

Python
data = [["sqatools"], ["is"], ["best"]]print(tuple(tuple(x) for x in data))
Output
(('sqatools',), ('is',), ('best',))

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Insert a string after every element

Python Practice › Tuple Programs › Program 55

Insert a string after every element

EasyTuple Programs · 55 of 70

Write a Python program to convert a tuple into a list with a string after every element.

Input
(1, 2, 3, 4), "sqatools"
Output
[1, 'sqatools', 2, 'sqatools', 3, 'sqatools', 4, 'sqatools']

Approach

  1. For each item add the item and the string.

Solution

Python
t = (1, 2, 3, 4)word = "sqatools"print([x for item in t for x in (item, word)])
Output
[1, 'sqatools', 2, 'sqatools', 3, 'sqatools', 4, 'sqatools']

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Flatten tuples into a string

Python Practice › Tuple Programs › Program 54

Flatten tuples into a string

EasyTuple Programs · 54 of 70

Write a Python program to flatten a list of tuples into a space-separated string.

Input
[("s", "q", "a"), ("t", "o"), ("o", "l", "s")]
Output
s q a t o o l s

Approach

  1. Join every item with spaces.

Solution

Python
data = [("s", "q", "a"), ("t", "o"), ("o", "l", "s")]print(" ".join(x for t in data for x in t))
Output
s q a t o o l s

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Flatten a list of lists into a tuple

Python Practice › Tuple Programs › Program 53

Flatten a list of lists into a tuple

EasyTuple Programs · 53 of 70

Write a Python program to flatten a list of lists into a tuple.

Input
[["s"], ["q"], ["a"], ["t"], ["o"], ["o"], ["l"], ["s"]]
Output
('s', 'q', 'a', 't', 'o', 'o', 'l', 's')

Approach

  1. A nested generator expression.

Solution

Python
data = [["s"], ["q"], ["a"], ["t"], ["o"], ["o"], ["l"], ["s"]]print(tuple(x for sub in data for x in sub))
Output
('s', 'q', 'a', 't', 'o', 'o', 'l', 's')

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Multiply the ith element of each tuple

Python Practice › Tuple Programs › Program 52

Multiply the ith element of each tuple

EasyTuple Programs · 52 of 70

Write a Python program to multiply the elements at index i of every tuple.

Input
[(4, 8, 3), (3, 4, 0), (1, 6, 2)], i = 1
Output
192

Approach

  1. math.prod(t[i] for t in data).

Solution

Python
import mathdata = [(4, 8, 3), (3, 4, 0), (1, 6, 2)]print(math.prod(t[1] for t in data))
Output
192

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs

Unique digits from tuples

Python Practice › Tuple Programs › Program 51

Unique digits from tuples

EasyTuple Programs · 51 of 70

Write a Python program to extract the unique digits of all numbers in a list of tuples.

Input
[(6, 87, 7), (4, 53), (11, 28, 3)]
Output
[1, 2, 3, 4, 5, 6, 7, 8]

Approach

  1. Join every number into one string and collect its distinct digits.

Solution

Python
data = [(6, 87, 7), (4, 53), (11, 28, 3)]digits = {int(ch) for t in data for n in t for ch in str(n)}print(sorted(digits))
Output
[1, 2, 3, 4, 5, 6, 7, 8]

Line by line

Line 2

The nested comprehension visits every number, then every digit of it.

Learn the concept

New to this topic? Read the lesson, then come back.

More tuple programs

← All 70 Tuple Programs