25 Python Tuple Questions with Solution
25 Python Tuple Coding Questions along with Explanations for each.
Let's get started ↓
Question 1: Find the length of a tuple.
# Solution 1:
def tuple_length(tup):
return len(tup)
# Solution 2:
tup = (1, 2, 3, 4, 5)
length = len(tup)
print(length) # Output: 5
Explanation: The len() function returns the number of elements in the tuple.
Question 2: Concatenate two tuples.
# Solution 1:
def concatenate_tuples(tup1, tup2):
return tup1 + tup2
# Solution 2:
tup1 = (1, 2, 3)
tup2 = (4, 5, 6)
result = tup1 + tup2
print(result) # Output: (1, 2, 3, 4, 5, 6)
Explanation: The + operator can be used to concatenate two tuples.
Question 3: Access an element in a tuple.
# Solution 1:
def access_element(tup, index):
return tup[index]
# Solution 2:
tup = (10, 20, 30, 40, 50)
element = tup[2]
print(element) # Output: 30
Explanation: Tuples are zero-indexed, so the third element has index 2.
Question 4: Count the occurrences of an element in a tuple.
# Solution 1:
def count_element(tup, value):
return tup.count(value)
# Solution 2:
tup = (1, 2, 2, 3, 2, 4, 2)
count = tup.count(2)
print(count) # Output: 4
Explanation: The count() method returns the number of occurrences of a given value in the tuple.
Question 5: Check if an element exists in a tuple.
# Solution 1:
def element_exists(tup, value):
return value in tup
# Solution 2:
tup = (10, 20, 30, 40, 50)
exists = 30 in tup
print(exists) # Output: True
Explanation: The in operator can be used to check if an element exists in the tuple.
Question 6: Convert a tuple to a string.
# Solution 1:
def tuple_to_string(tup):
return str(tup)
# Solution 2:
tup = (1, 2, 3)
string = str(tup)
print(string) # Output: "(1, 2, 3)"
Explanation: The str() function converts the tuple to its string representation.
Question 7: Find the index of an element in a tuple.
# Solution 1:
def find_index(tup, value):
return tup.index(value)
# Solution 2:
tup = (10, 20, 30, 40, 50)
index = tup.index(30)
print(index) # Output: 2
Explanation: The index() method returns the index of the first occurrence of the given value in the tuple.
Question 8: Convert a list to a tuple.
# Solution 1:
def list_to_tuple(lst):
return tuple(lst)
# Solution 2:
lst = [1, 2, 3, 4, 5]
tup = tuple(lst)
print(tup) # Output: (1, 2, 3, 4, 5)
Explanation: The tuple() constructor can be used to convert a list to a tuple.
Question 9: Iterate through a tuple using a loop.
# Solution:
tup = (10, 20, 30, 40, 50)
for element in tup:
print(element)
Explanation: A for loop can be used to iterate through the elements of a tuple.
Question 10: Find the maximum and minimum elements in a tuple.
# Solution:
def max_min_elements(tup):
return max(tup), min(tup)
tup = (5, 8, 2, 10, 3)
max_element, min_element = max_min_elements(tup)
print("Max:", max_element) # Output: Max: 10
print("Min:", min_element) # Output: Min: 2
Explanation: The max() and min() functions return the maximum and minimum elements in the tuple, respectively.
Question 11: Convert a tuple of strings to a single string.
# Solution:
def tuple_to_single_string(tup):
return ''.join(tup)
tup = ('H', 'e', 'l', 'l', 'o')
result = tuple_to_single_string(tup)
print(result) # Output: "Hello"
Explanation: The join() method can be used to concatenate elements of the tuple into a single string.
Question 12: Remove an element from a tuple.
# Solution:
def remove_element(tup, value):
return tuple(x for x in tup if x != value)
tup = (1, 2, 3, 4, 3, 5)
new_tup = remove_element(tup, 3)
print(new_tup) # Output: (1, 2, 4, 5)
Explanation: A new tuple is created using a generator expression that excludes the specified value.
Question 13: Find the common elements between two tuples.
Recommended by LinkedIn
# Solution:
def common_elements(tup1, tup2):
return tuple(x for x in tup1 if x in tup2)
tup1 = (1, 2, 3, 4)
tup2 = (3, 4, 5, 6)
common = common_elements(tup1, tup2)
print(common) # Output: (3, 4)
Explanation: The generator expression creates a new tuple containing elements that are common between the two input tuples.
Question 14: Sort a tuple of integers.
# Solution:
def sort_tuple(tup):
return tuple(sorted(tup))
tup = (5, 3, 8, 1, 2)
sorted_tup = sort_tuple(tup)
print(sorted_tup) # Output: (1, 2, 3, 5, 8)
Explanation: The sorted() function is used to sort the elements of the tuple, and then a new tuple is created from the sorted list.
Question 15: Find the sum of all elements in a tuple.
# Solution:
def sum_tuple(tup):
return sum(tup)
tup = (1, 2, 3, 4, 5)
total = sum_tuple(tup)
print(total) # Output: 15
Explanation: The sum() function returns the sum of all elements in the tuple.
**Question
16:** Merge two tuples and remove duplicates.
# Solution:
def merge_and_remove_duplicates(tup1, tup2):
return tuple(set(tup1).union(tup2))
tup1 = (1, 2, 3)
tup2 = (3, 4, 5)
result = merge_and_remove_duplicates(tup1, tup2)
print(result) # Output: (1, 2, 3, 4, 5)
Explanation: The set() function is used to remove duplicates, and then the union() method combines the two sets into one set.
Question 17: Find the first and last elements of a tuple.
# Solution:
def first_last_elements(tup):
return tup[0], tup[-1]
tup = (10, 20, 30, 40, 50)
first, last = first_last_elements(tup)
print("First:", first) # Output: First: 10
print("Last:", last) # Output: Last: 50
Explanation: The first element is accessed using index 0, and the last element is accessed using index -1.
Question 18: Convert a tuple of integers to a tuple of strings.
# Solution:
def int_to_str_tuple(tup):
return tuple(str(x) for x in tup)
tup = (1, 2, 3, 4, 5)
str_tup = int_to_str_tuple(tup)
print(str_tup) # Output: ('1', '2', '3', '4', '5')
Explanation: A new tuple is created by converting each integer element to its string representation.
Question 19: Count the number of even and odd numbers in a tuple.
# Solution:
def count_even_odd(tup):
even_count = sum(1 for x in tup if x % 2 == 0)
odd_count = len(tup) - even_count
return even_count, odd_count
tup = (1, 2, 3, 4, 5, 6, 7, 8, 9)
even, odd = count_even_odd(tup)
print("Even:", even) # Output: Even: 4
print("Odd:", odd) # Output: Odd: 5
Explanation: A generator expression is used to count even elements, and odd count is calculated based on total count and even count.
Question 20: Find the product of all elements in a tuple.
# Solution:
def product_tuple(tup):
product = 1
for x in tup:
product *= x
return product
tup = (1, 2, 3, 4, 5)
result = product_tuple(tup)
print(result) # Output: 120
Explanation: The product is calculated by iterating through the elements and continuously multiplying them.
Question 21: Split a tuple into equal parts.
# Solution:
def split_tuple(tup, n):
avg = len(tup) // n
split_result = [tup[i:i+avg] for i in range(0, len(tup), avg)]
return tuple(split_result)
tup = (1, 2, 3, 4, 5, 6)
n = 2
result = split_tuple(tup, n)
print(result) # Output: ((1, 2, 3), (4, 5, 6))
Explanation: The tuple is split into n equal parts using list comprehension and then converted back to a tuple.
Question 22: Find the frequency of each element in a tuple.
# Solution:
def element_frequency(tup):
freq_dict = {}
for element in tup:
freq_dict[element] = freq_dict.get(element, 0) + 1
return freq_dict
tup = (1, 2, 2, 3, 2, 4, 2)
frequency = element_frequency(tup)
print(frequency) # Output: {1: 1, 2: 4, 3: 1, 4: 1}
Explanation: A dictionary is used to store the frequency of each element in the tuple.
Question 23: Find the difference between two tuples.
# Solution:
def tuple_difference(tup1, tup2):
return tuple(x for x in tup1 if x not in tup2)
tup1 = (1, 2, 3, 4, 5)
tup2 = (3, 4, 5, 6)
difference = tuple_difference(tup1, tup2)
print(difference) # Output: (1, 2)
Explanation: A new tuple is created by including only those elements from the first tuple that are not present in the second tuple.
Question 24: Find the second largest element in a tuple.
# Solution:
def second_largest(tup):
sorted_tup = sorted(tup)
return sorted_tup[-2]
tup = (10, 5, 8, 20, 15)
second_largest_element = second_largest(tup)
print(second_largest_element) # Output: 15
Explanation: The tuple is sorted in ascending order, and the second-to-last element is the second largest.
Question 25: Check if a tuple is a subset of another tuple.
# Solution:
def is_subset(sub, main):
return all(x in main for x in sub)
main_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9)
sub_tuple = (2, 4, 6)
result = is_subset(sub_tuple, main_tuple)
print(result) # Output: True
Explanation: The all() function is used to check if all elements in the subset tuple are present in the main tuple.