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

Tuples with more than 3 elements

Python Practice › Tuple Programs › Program 33

Tuples with more than 3 elements

EasyTuple Programs · 33 of 70

Write a Python program to keep tuples with more than 3 elements.

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

Approach

  1. len(t) > 3.

Solution

Python
data = [(1, 4), (4, 5, 6), (2,), (7, 6, 8, 9), (3, 5, 6, 0, 1)]print([t for t in data if len(t) > 3])
Output
[(7, 6, 8, 9), (3, 5, 6, 0, 1)]

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Frequency of tuple elements

Python Practice › Tuple Programs › Program 32

Frequency of tuple elements

EasyTuple Programs · 32 of 70

Write a Python program to count how often each element appears in a tuple.

Input
("a", "b", "c", "d", "b", "a", "b")
Output
{'a': 2, 'b': 3, 'c': 1, 'd': 1}

Approach

  1. Counter.

Solution

Python
from collections import Counterprint(dict(Counter(("a", "b", "c", "d", "b", "a", "b"))))
Output
{'a': 2, 'b': 3, 'c': 1, 'd': 1}

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Sort tuples by length

Python Practice › Tuple Programs › Program 31

Sort tuples by length

EasyTuple Programs · 31 of 70

Write a Python program to sort a list of tuples by length.

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

Approach

  1. key=len.

Solution

Python
print(sorted([(4, 5, 6), (6,), (2, 3), (6, 7, 8, 9, 0)], key=len))
Output
[(6,), (2, 3), (4, 5, 6), (6, 7, 8, 9, 0)]

Learn the concept

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

More tuple programs

← All 70 Tuple Programs