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

Tuple of pairs to a dictionary

Python Practice › Tuple Programs › Program 23

Tuple of pairs to a dictionary

EasyTuple Programs · 23 of 70

Write a Python program to convert a tuple of (value, key) pairs into a dictionary keyed by the second item.

Input
((5, "s"), (6, "l"))
Output
{'s': 5, 'l': 6}

Approach

  1. A dict comprehension swaps the order.

Solution

Python
pairs = ((5, "s"), (6, "l"))print({key: value for value, key in pairs})
Output
{'s': 5, 'l': 6}

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Length of a tuple

Python Practice › Tuple Programs › Program 22

Length of a tuple

EasyTuple Programs · 22 of 70

Write a Python program to find the length of a tuple.

Input
("v", "i", "r", "a", "t")
Output
5

Approach

  1. len().

Solution

Python
print(len(("v", "i", "r", "a", "t")))
Output
5

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Index of an element

Python Practice › Tuple Programs › Program 21

Index of an element

EasyTuple Programs · 21 of 70

Write a Python program to find the index of an element in a tuple.

Input
("s", "q", "a", "t", "o", "o", "l", "s"), "a"
Output
2

Approach

  1. index().

Solution

Python
t = ("s", "q", "a", "t", "o", "o", "l", "s")print(t.index("a"))
Output
2

Learn the concept

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

More tuple programs

← All 70 Tuple Programs