Questions Related to Python Programs
Updated on June 2, 2025 | By Learnzy Academy
Q1. How to check if a tuple is a subset of another tuple in python ?
Use Python’s all() function to test if every element of the first tuple exists in the second tuple.
Example:-
tuple1 = (1, 2, 3)
tuple2 = (5, 3, 2, 1, 4)
is_subset = all(item in tuple2 for item in tuple1)
print(is_subset) # True
Q2. How to Merge two tuples and remove duplicates in python.
Steps:--
- Tuples themselves don’t support removing duplicates directly because they are immutable.
- So, first concatenate the tuples.
- Convert the concatenated tuple into a set to remove duplicates (sets store unique items).
- Convert back to a tuple if you want the result as a tuple.
tuple1 = (1, 2, 3, 4)
tuple2 = (3, 4, 5, 6)
# Merge tuples
merged = tuple1 + tuple2
# Remove duplicates by converting to set, then back to tuple
result = tuple(set(merged))
print(result)Q3. How to find the first and last elements of a tuple in python?
Since tuples are ordered collections, you can access elements by their index. The first element is at index 0. And the last element is at index -1.
Example:--
my_tuple = (10, 20, 30, 40, 50)
first_element = my_tuple[0]
last_element = my_tuple[-1]
print("First element:", first_element)
print("Last element:", last_element)
Q4. How to check if two lists are equal?
To check if two lists are equal (same elements in the same order), use the == operator.
Example :--
list1 = [1, 2, 3]
list2 = [1, 2, 3]
if list1 == list2:
print("Lists are equal")
else:
print("Lists are not equal")
Q5. Find common elements in two lists
Using set intersection we can get the common element b/w two list. This is the fast way to get unique common elements, but order is not preserved
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
common = list(set(list1) & set(list2))
print(common) # Output: [3, 4]