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

Remove duplicates from a tuple

Python Practice › Tuple Programs › Program 50

Remove duplicates from a tuple

EasyTuple Programs · 50 of 70

Write a Python program to remove duplicates from a tuple, keeping the order.

Input
(6, 4, 9, 0, 2, 6, 1, 3, 4)
Output
(6, 4, 9, 0, 2, 1, 3)

Approach

  1. tuple(dict.fromkeys(t)).

Solution

Python
print(tuple(dict.fromkeys((6, 4, 9, 0, 2, 6, 1, 3, 4))))
Output
(6, 4, 9, 0, 2, 1, 3)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Tuples with no negative elements

Python Practice › Tuple Programs › Program 49

Tuples with no negative elements

EasyTuple Programs · 49 of 70

Write a Python program to find tuples whose elements are all zero or positive.

Input
[(1, 7), (-4, -5), (0, 6), (-1, 3)]
Output
[(1, 7), (0, 6)]

Approach

  1. all(x >= 0 for x in t).

Solution

Python
data = [(1, 7), (-4, -5), (0, 6), (-1, 3)]print([t for t in data if all(x >= 0 for x in t)])
Output
[(1, 7), (0, 6)]

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Tuples with a negative element

Python Practice › Tuple Programs › Program 48

Tuples with a negative element

EasyTuple Programs · 48 of 70

Write a Python program to find tuples that contain at least one negative number.

Input
[(1, 7), (-4, -5), (0, 6), (-1, 3)]
Output
[(-4, -5), (-1, 3)]

Approach

  1. any(x < 0 for x in t).

Solution

Python
data = [(1, 7), (-4, -5), (0, 6), (-1, 3)]print([t for t in data if any(x < 0 for x in t)])
Output
[(-4, -5), (-1, 3)]

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Tuples whose items are all divisible by 5

Python Practice › Tuple Programs › Program 47

Tuples whose items are all divisible by 5

EasyTuple Programs · 47 of 70

Write a Python program to find the tuples whose elements are all divisible by a number.

Input
[(10, 5, 15), (25, 6, 35), (20, 10)], number 5
Output
[(10, 5, 15), (20, 10)]

Approach

  1. all(x % k == 0 for x in t).

Solution

Python
data = [(10, 5, 15), (25, 6, 35), (20, 10)]k = 5print([t for t in data if all(x % k == 0 for x in t)])
Output
[(10, 5, 15), (20, 10)]

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

List of tuples to list of lists

Python Practice › Tuple Programs › Program 46

List of tuples to list of lists

EasyTuple Programs · 46 of 70

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

Input
[(1, 5), (7, 8), (4, 0)]
Output
[[1, 5], [7, 8], [4, 0]]

Approach

  1. list(t) for each tuple.

Solution

Python
print([list(t) for t in [(1, 5), (7, 8), (4, 0)]])
Output
[[1, 5], [7, 8], [4, 0]]

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Element-wise sum of tuples

Python Practice › Tuple Programs › Program 45

Element-wise sum of tuples

EasyTuple Programs · 45 of 70

Write a Python program to add several tuples element by element.

Input
(1, 6, 7), (4, 3, 0), (2, 6, 8)
Output
(7, 15, 15)

Approach

  1. zip(*tuples) groups items by position; sum each group.

Solution

Python
tuples = [(1, 6, 7), (4, 3, 0), (2, 6, 8)]print(tuple(sum(column) for column in zip(*tuples)))
Output
(7, 15, 15)

Line by line

Line 2

zip(*tuples) gives (1, 4, 2), (6, 3, 6) and (7, 0, 8).

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Tuple of digits to a number

Python Practice › Tuple Programs › Program 44

Tuple of digits to a number

EasyTuple Programs · 44 of 70

Write a Python program to convert a tuple of digits into a number.

Input
(4, 5, 3, 8)
Output
4538

Approach

  1. Build the number with n * 10 + digit.

Solution

Python
number = 0for d in (4, 5, 3, 8):    number = number * 10 + dprint(number)
Output
4538

Learn the concept

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

More tuple programs

← All 70 Tuple Programs