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

Tuple of strings to integers

Python Practice › Tuple Programs › Program 43

Tuple of strings to integers

EasyTuple Programs · 43 of 70

Write a Python program to convert a tuple of numeric strings to integers, ignoring stray commas.

Input
("4563", "68", "1,")
Output
(4563, 68, 1)

Approach

  1. Strip the commas, then int() each value.

Solution

Python
t = ("4563", "68", "1,")print(tuple(int(s.replace(",", "")) for s in t))
Output
(4563, 68, 1)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

String to a tuple of characters

Python Practice › Tuple Programs › Program 42

String to a tuple of characters

EasyTuple Programs · 42 of 70

Write a Python program to convert a string into a tuple of characters.

Input
"Sqatools"
Output
('S', 'q', 'a', 't', 'o', 'o', 'l', 's')

Approach

  1. tuple(text).

Solution

Python
print(tuple("Sqatools"))
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

Product of a tuple

Python Practice › Tuple Programs › Program 41

Product of a tuple

EasyTuple Programs · 41 of 70

Write a Python program to multiply all the elements of a tuple.

Input
(5, 4, 3, 1)
Output
60

Approach

  1. math.prod().

Solution

Python
import mathprint(math.prod((5, 4, 3, 1)))
Output
60

Learn the concept

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

More tuple programs

← All 70 Tuple Programs