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

Count elements before a tuple

Python Practice › Tuple Programs › Program 40

Count elements before a tuple

EasyTuple Programs · 40 of 70

Write a Python program to count the elements of a list until the first tuple is reached.

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

Approach

  1. Count until isinstance(x, tuple).

Solution

Python
data = [1, 6, 8, 5, (4, 6, 8, 0), 5, 4]count = 0for x in data:    if isinstance(x, tuple):        break    count += 1print(count)
Output
4

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Sort tuples by a float element

Python Practice › Tuple Programs › Program 39

Sort tuples by a float element

EasyTuple Programs · 39 of 70

Write a Python program to sort tuples by their float element.

Input
[(3, 5.6), (6, 2.3), (1, 1.8)]
Output
[(1, 1.8), (6, 2.3), (3, 5.6)]

Approach

  1. key=lambda t: t[1].

Solution

Python
print(sorted([(3, 5.6), (6, 2.3), (1, 1.8)], key=lambda t: t[1]))
Output
[(1, 1.8), (6, 2.3), (3, 5.6)]

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Remove empty tuples

Python Practice › Tuple Programs › Program 38

Remove empty tuples

EasyTuple Programs · 38 of 70

Write a Python program to remove empty tuples (and tuples holding only an empty string) from a list.

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

Approach

  1. Keep tuples that contain at least one truthy value.

Solution

Python
data = [("",), ("a", "b"), ("a", "b", "c"), ("d",), ()]print([t for t in data if any(t)])
Output
[('a', 'b'), ('a', 'b', 'c'), ('d',)]

Line by line

Line 2

any(()) is False and any(("",)) is False, so both are removed.

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Tuple to a string

Python Practice › Tuple Programs › Program 37

Tuple to a string

EasyTuple Programs · 37 of 70

Write a Python program to convert a tuple to a string.

Input
(4, 1, 7, 5)
Output
The given tuple is (4, 1, 7, 5)
4175

Approach

  1. str(t) gives its printed form; join gives just the digits.

Solution

Python
t = (4, 1, 7, 5)print("The given tuple is " + str(t))print("".join(map(str, t)))
Output
The given tuple is (4, 1, 7, 5)
4175

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Are all tuple elements distinct?

Python Practice › Tuple Programs › Program 36

Are all tuple elements distinct?

EasyTuple Programs · 36 of 70

Write a Python program to test whether all elements of a tuple are distinct.

Input
(1, 2, 3, 4), (3, 4, 5, 4, 6, 3)
Output
(1, 2, 3, 4) is distinct
(3, 4, 5, 4, 6, 3) is not distinct

Approach

  1. Compare len(set(t)) with len(t).

Solution

Python
for t in [(1, 2, 3, 4), (3, 4, 5, 4, 6, 3)]:    print(t, "is distinct" if len(set(t)) == len(t) else "is not distinct")
Output
(1, 2, 3, 4) is distinct
(3, 4, 5, 4, 6, 3) is not distinct

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Tuple at a given position

Python Practice › Tuple Programs › Program 35

Tuple at a given position

EasyTuple Programs · 35 of 70

Write a Python program to get the tuple at index i of a list of tuples.

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

Approach

  1. Index the list.

Solution

Python
data = [(1, 2, 3), (6, 5, 4), (7, 6, 8), (9, 0, 1)]print(data[3])
Output
(9, 0, 1)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

How often each tuple appears

Python Practice › Tuple Programs › Program 34

How often each tuple appears

EasyTuple Programs · 34 of 70

Write a Python program to count how many times each tuple appears in a list.

Input
[("s", "q"), ("t", "o", "o", "l"), ("p", "y"), ("s", "q")]
Output
{('s', 'q'): 2, ('t', 'o', 'o', 'l'): 1, ('p', 'y'): 1}

Approach

  1. Tuples are hashable, so Counter counts them directly.

Solution

Python
from collections import Counterdata = [("s", "q"), ("t", "o", "o", "l"), ("p", "y"), ("s", "q")]print(dict(Counter(data)))
Output
{('s', 'q'): 2, ('t', 'o', 'o', 'l'): 1, ('p', 'y'): 1}

Learn the concept

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

More tuple programs

← All 70 Tuple Programs