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

Slice a tuple

Python Practice › Tuple Programs › Program 20

Slice a tuple

EasyTuple Programs · 20 of 70

Write a Python program to slice a tuple.

Input
(5, 7, 3, 4, 9, 0, 2)
Output
(5, 7, 3)
(3, 4, 9)
(2, 0, 9, 4, 3, 7, 5)

Approach

  1. t[start:stop] works like list slicing.

Solution

Python
t = (5, 7, 3, 4, 9, 0, 2)print(t[:3])print(t[2:5])print(t[::-1])
Output
(5, 7, 3)
(3, 4, 9)
(2, 0, 9, 4, 3, 7, 5)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Remove an item from a tuple

Python Practice › Tuple Programs › Program 19

Remove an item from a tuple

EasyTuple Programs · 19 of 70

Write a Python program to remove an item from a tuple.

Input
("p", "y", "t", "h", "o", "n"), remove "h"
Output
('p', 'y', 't', 'o', 'n')

Approach

  1. Build a new tuple without the item (tuples cannot be changed).

Solution

Python
t = ("p", "y", "t", "h", "o", "n")i = t.index("h")print(t[:i] + t[i + 1:])
Output
('p', 'y', 't', 'o', 'n')

Line by line

Line 2

Find the position, then join the slices around it. This removes only the first “h”.

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

List to tuple, doubled

Python Practice › Tuple Programs › Program 18

List to tuple, doubled

EasyTuple Programs · 18 of 70

Write a Python program to convert a list into a tuple with each element multiplied by 2.

Input
[12, 65, 34, 77]
Output
(24, 130, 68, 154)

Approach

  1. tuple(x * 2 for x in items).

Solution

Python
print(tuple(x * 2 for x in [12, 65, 34, 77]))
Output
(24, 130, 68, 154)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Multiply adjacent elements

Python Practice › Tuple Programs › Program 16

Multiply adjacent elements

EasyTuple Programs · 16 of 70

Write a Python program to multiply each pair of neighbouring elements of a tuple.

Input
(1, 2, 3, 4)
Output
(2, 6, 12)

Approach

  1. zip(t, t[1:]) gives the neighbours.

Solution

Python
t = (1, 2, 3, 4)print(tuple(a * b for a, b in zip(t, t[1:])))
Output
(2, 6, 12)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Tuple of squares from a list

Python Practice › Tuple Programs › Program 15

Tuple of squares from a list

EasyTuple Programs · 15 of 70

Write a Python program to create a tuple of the squares of a list’s elements.

Input
[1, 9, 5, 7, 6]
Output
(1, 81, 25, 49, 36)

Approach

  1. Pass a generator expression to tuple().

Solution

Python
print(tuple(n * n for n in [1, 9, 5, 7, 6]))
Output
(1, 81, 25, 49, 36)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Sum of a tuple

Python Practice › Tuple Programs › Program 13

Sum of a tuple

EasyTuple Programs · 13 of 70

Write a Python program to find the sum of the elements of a tuple.

Input
(4, 6, 2)
Output
12

Approach

  1. sum().

Solution

Python
print(sum((4, 6, 2)))
Output
12

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Add a list’s items to a tuple

Python Practice › Tuple Programs › Program 12

Add a list's items to a tuple

EasyTuple Programs · 12 of 70

Write a Python program to add the items of a list to a tuple.

Input
list [12, 67], tuple (6, 8, 4)
Output
(6, 8, 4, 12, 67)

Approach

  1. Convert the list to a tuple and concatenate.

Solution

Python
items = [12, 67]t = (6, 8, 4)t = t + tuple(items)print(t)
Output
(6, 8, 4, 12, 67)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs