Last element with negative indexing

Python Practice › Tuple Programs › Program 70

Last element with negative indexing

EasyTuple Programs · 70 of 70

Write a Python program to get the last element of a tuple with negative indexing.

Input
("p", "y", "t", "o", "n")
Output
n

Approach

  1. t[-1].

Solution

Python
print(("p", "y", "t", "o", "n")[-1])
Output
n

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Is the value a tuple?

Python Practice › Tuple Programs › Program 69

Is the value a tuple?

EasyTuple Programs · 69 of 70

Write a Python program that returns True if a value is a tuple.

Input
(7, 4, 9, 2, 0), [7, 4]
Output
(7, 4, 9, 2, 0) True
[7, 4] False

Approach

  1. isinstance(x, tuple).

Solution

Python
for value in [(7, 4, 9, 2, 0), [7, 4]]:    print(value, isinstance(value, tuple))
Output
(7, 4, 9, 2, 0) True
[7, 4] False

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Swap two tuples

Python Practice › Tuple Programs › Program 68

Swap two tuples

EasyTuple Programs · 68 of 70

Write a Python program to swap two tuple variables.

Input
A = (7, 4, 9), B = (3,)
Output
A = (3,)
B = (7, 4, 9)

Approach

  1. Tuple unpacking swaps the names.

Solution

Python
A, B = (7, 4, 9), (3,)A, B = B, Aprint("A =", A)print("B =", B)
Output
A = (3,)
B = (7, 4, 9)

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Average of a tuple

Python Practice › Tuple Programs › Program 67

Average of a tuple

EasyTuple Programs · 67 of 70

Write a Python program to calculate the average of a tuple’s elements.

Input
(5, 3, 9, 6)
Output
5.75

Approach

  1. sum / len.

Solution

Python
t = (5, 3, 9, 6)print(sum(t) / len(t))
Output
5.75

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Binary tuple to an integer

Python Practice › Tuple Programs › Program 65

Binary tuple to an integer

EasyTuple Programs · 65 of 70

Write a Python program to convert a tuple of binary digits into an integer.

Input
(1, 0, 0)
Output
4

Approach

  1. Join the digits and use int(text, 2).

Solution

Python
bits = (1, 0, 0)print(int("".join(map(str, bits)), 2))
Output
4

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Count unique tuples

Python Practice › Tuple Programs › Program 66

Count unique tuples

EasyTuple Programs · 66 of 70

Write a Python program to count the distinct tuples in a list.

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

Approach

  1. len(set(data)).

Solution

Python
print(len(set([(8, 9), (4, 7), (3, 6), (8, 9)])))
Output
3

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Common tuples ignoring order

Python Practice › Tuple Programs › Program 64

Common tuples ignoring order

MediumTuple Programs · 64 of 70

Write a Python program to find the tuples that appear in both lists, treating (3, 9) and (9, 3) as the same.

Input
A = [(1, 5), (4, 8), (3, 9)], B = [(9, 3), (5, 6), (5, 1), (0, 4)]
Output
{(3, 9), (1, 5)}

Approach

  1. Compare sorted versions of each tuple.

Solution

Python
A = [(1, 5), (4, 8), (3, 9)]B = [(9, 3), (5, 6), (5, 1), (0, 4)]norm_b = {tuple(sorted(t)) for t in B}print({t for t in A if tuple(sorted(t)) in norm_b})
Output
{(3, 9), (1, 5)}

Line by line

Line 3

Sorting each tuple makes (9, 3) and (3, 9) identical.

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Order tuples by an external list

Python Practice › Tuple Programs › Program 63

Order tuples by an external list

EasyTuple Programs · 63 of 70

Write a Python program to order tuples by the order of their keys in another list.

Input
[("very", 8), ("i", 6), ("am", 5), ("happy", 0)], order ["i", "am", "very", "happy"]
Output
[('i', 6), ('am', 5), ('very', 8), ('happy', 0)]

Approach

  1. Sort by each key’s position in the order list.

Solution

Python
data = [("very", 8), ("i", 6), ("am", 5), ("happy", 0)]order = ["i", "am", "very", "happy"]position = {key: i for i, key in enumerate(order)}print(sorted(data, key=lambda t: position[t[0]]))
Output
[('i', 6), ('am', 5), ('very', 8), ('happy', 0)]

Line by line

Line 3

A position lookup dict is faster than calling order.index for every item.

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Concatenate or nest two tuples

Python Practice › Tuple Programs › Program 62

Concatenate or nest two tuples

EasyTuple Programs · 62 of 70

Write a Python program to concatenate two tuples, and to nest them inside one tuple.

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

Approach

  1. + joins; (a, b) nests.

Solution

Python
a = ("s", "q", "a")b = ("t", "o", "o", "l")print(a + b)print((a, b))
Output
('s', 'q', 'a', 't', 'o', 'o', 'l')
(('s', 'q', 'a'), ('t', 'o', 'o', 'l'))

Learn the concept

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

More tuple programs

← All 70 Tuple Programs

Sort by each tuple’s minimum

Python Practice › Tuple Programs › Program 61

Sort by each tuple's minimum

EasyTuple Programs · 61 of 70

Write a Python program to sort tuples by their minimum value.

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

Approach

  1. key=min.

Solution

Python
print(sorted([(1, 5, 7), (3, 4, 2), (4, 9, 0)], key=min))
Output
[(4, 9, 0), (1, 5, 7), (3, 4, 2)]

Learn the concept

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

More tuple programs

← All 70 Tuple Programs