Python Tuple Problems and Solutions: Mastering Immutable Sequences

In the previous section we discussed the basics of the tuple. Now let us take a look at some more Problems.

 

1. Write a Python Program to remove duplicates from a tuple (1,2,2,3,4,4,5)

				
					t = (1, 2, 2, 3, 4, 4, 5)
t_unique = tuple(set(t))
print(t_unique)

# (1, 2, 3, 4, 5)

				
			

2. Count Occurrences

Count how many times 2 appears in (1, 2, 3, 2, 4, 2, 5).

				
					t = (1, 2, 3, 2, 4, 2, 5)
print(t.count(2))

# 3
				
			

3. Find Maximum and Minimum

Find the largest and smallest number in (12, 45, 7, 89, 23).

				
					t = (12, 45, 7, 89, 23)
print("Max:", max(t))
print("Min:", min(t))

# Max: 89
# Min: 7

				
			

4. Find Sum of Elements

Find the sum of all elements in (10, 20, 30, 40).

				
					t = (10, 20, 30, 40)
print(sum(t))

# 100
				
			

5. Student Records System (Immutable Database)

Write a Python Program to store student records as tuples in the format:

(RollNo, Name, Marks)
 

Then,

  1. Find the topper

  2. Find the average marks

  3. Display students scoring above average

				
					students = (
    (101, "John", 88),
    (102, "Mary", 92),
    (103, "Anil", 76),
    (104, "Alva", 95),
    (105, "Cyril", 84)
)

# Topper
topper = max(students, key=lambda x: x[2])
print("Topper:", topper)

# Average
avg = sum(s[2] for s in students) / len(students)
print("Average Marks:", avg)

# Above average students
above_avg = tuple(s for s in students if s[2] > avg)
print("Above Average:", above_avg)

# Topper: (104, 'Alva', 95)
# Average Marks: 87.0
# Above Average: ((101, 'John', 88), (102, 'Mary', 92), (104, 'Alva', 95))

				
			

6. Tuple-Based Matrix

Represent a matrix using nested tuples and compute:

  • Transpose of a matrix

((1,2,3),
(4,5,6),
(7,8,9))

				
					matrix = (
    (1, 2, 3),
    (4, 5, 6),
    (7, 8, 9)
)

transpose = tuple(zip(*matrix))
print("Transpose:", transpose)

# Transpose: ((1, 4, 7), (2, 5, 8), (3, 6, 9))

				
			

7. Priority Scheduling (Tuple Sorting Application)

Each task stored as:

(Priority, TaskName)

Lower number = higher priority. Execute tasks in order.

				
					tasks = (
    (3, "Write Report"),
    (1, "Fix Bug"),
    (2, "Team Meeting"),
    (1, "Client Call")
)

sorted_tasks = tuple(sorted(tasks))
print("Execution Order:")
for task in sorted_tasks:
    print(task[1])
    
# Execution Order:
# Client Call
# Fix Bug
# Team Meeting
# Write Report

				
			

8. Airline Reservation Snapshot

Each booking is stored as:

(PassengerName, FlightNo, SeatNo, Price)

Tasks:

  1. Calculate total revenue.

  2. Find all passengers of a particular flight.

  3. Find the highest ticket price.

				
					bookings = (
    ("Alva", "AI101", "12A", 6500),
    ("Sara", "AI101", "14C", 7200),
    ("John", "AI202", "10B", 5400),
    ("Megha", "AI101", "16D", 8000)
)

# Total revenue
revenue = sum(b[3] for b in bookings)
print("Total Revenue:", revenue)

# Passengers in AI101
ai101_passengers = tuple(b[0] for b in bookings if b[1] == "AI101")
print("AI101 Passengers:", ai101_passengers)

# Highest ticket price
highest = max(bookings, key=lambda x: x[3])
print("Highest Ticket:", highest)

# Total Revenue: 27100
# AI101 Passengers: ('Alva', 'Sara', 'Megha')
# Highest Ticket: ('Megha', 'AI101', '16D', 8000)