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

Sort tuples by the second item

Python Practice › Tuple Programs › Program 30

Sort tuples by the second item

EasyTuple Programs · 30 of 70

Write a Python program to sort a list of tuples by their second item.

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

Approach

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

Solution

Python
data = [(1, 5), (7, 8), (4, 0), (3, 6)]print(sorted(data, key=lambda t: t[1]))
Output
[(4, 0), (1, 5), (3, 6), (7, 8)]

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Sort tuples by the first item

Python Practice › Tuple Programs › Program 29

Sort tuples by the first item

EasyTuple Programs · 29 of 70

Write a Python program to sort a list of tuples by their first item.

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

Approach

  1. Tuples sort by their first item by default.

Solution

Python
print(sorted([(1, 5), (7, 8), (4, 0), (3, 6)]))
Output
[(1, 5), (3, 6), (4, 0), (7, 8)]

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Remove tuples that are all None

Python Practice › Tuple Programs › Program 28

Remove tuples that are all None

EasyTuple Programs · 28 of 70

Write a Python program to remove tuples in which every element is None.

Input
[(None,), (None, None), (5, 4), (1, 6, 7), (None, 1)]
Output
[(5, 4), (1, 6, 7), (None, 1)]

Approach

  1. Keep tuples where not all items are None.

Solution

Python
data = [(None,), (None, None), (5, 4), (1, 6, 7), (None, 1)]print([t for t in data if not all(x is None for x in t)])
Output
[(5, 4), (1, 6, 7), (None, 1)]

Line by line

Line 2

(None, 1) stays, because only one of its items is None.

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Remove tuples containing None

Python Practice › Tuple Programs › Program 27

Remove tuples containing None

EasyTuple Programs · 27 of 70

Write a Python program to remove tuples that contain None.

Input
[(None, 2), (None, None), (5, 4), (1, 6, 7)]
Output
[(5, 4), (1, 6, 7)]

Approach

  1. Keep tuples where None not in t.

Solution

Python
data = [(None, 2), (None, None), (5, 4), (1, 6, 7)]print([t for t in data if None not in t])
Output
[(5, 4), (1, 6, 7)]

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Remove tuples of a given length

Python Practice › Tuple Programs › Program 26

Remove tuples of a given length

EasyTuple Programs · 26 of 70

Write a Python program to remove tuples of length i from a list.

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

Approach

  1. Keep tuples whose length is not i.

Solution

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

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Group a list of pairs into a dictionary

Python Practice › Tuple Programs › Program 25

Group a list of pairs into a dictionary

EasyTuple Programs · 25 of 70

Write a Python program to group (key, value) tuples into a dictionary of lists.

Input
[("s", 2), ("q", 1), ("a", 1), ("s", 3), ("q", 2), ("a", 4)]
Output
{'s': [2, 3], 'q': [1, 2], 'a': [1, 4]}

Approach

  1. setdefault(key, []) creates the list on first use.

Solution

Python
data = [("s", 2), ("q", 1), ("a", 1), ("s", 3), ("q", 2), ("a", 4)]result = {}for key, value in data:    result.setdefault(key, []).append(value)print(result)
Output
{'s': [2, 3], 'q': [1, 2], 'a': [1, 4]}

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Reverse a tuple

Python Practice › Tuple Programs › Program 24

Reverse a tuple

EasyTuple Programs · 24 of 70

Write a Python program to reverse a tuple.

Input
(4, 6, 8, 3, 1)
Output
(1, 3, 8, 6, 4) (1, 3, 8, 6, 4)

Approach

  1. [::-1] or tuple(reversed(t)).

Solution

Python
t = (4, 6, 8, 3, 1)print(t[::-1], tuple(reversed(t)))
Output
(1, 3, 8, 6, 4) (1, 3, 8, 6, 4)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs