Find Largest/Smallest Number in List/Tuple

AIM

To write a Python program that finds the largest and smallest numbers in a list/tuple, including second largest and second smallest elements.

Key Concepts:

  • List Sorting: Using sort() method to arrange elements
  • List Indexing: Accessing elements by position
  • Built-in Functions: Using max() and min() functions

ALGORITHM

Method 1 - Using Sorting:

  1. Start
  2. Initialize a list with numbers
  3. Find the length of the list
  4. Sort the list in ascending order using sort()
  5. Access first element for smallest (index 0)
  6. Access last element for largest (index length-1)
  7. Access second element for second smallest (index 1)
  8. Access second last element for second largest (index length-2)
  9. Display all results
  10. Stop

Method 2 - Using Built-in Functions:

  1. Use max() function to find largest element
  2. Use min() function to find smallest element
  3. Remove max element and find max again for second largest
  4. Remove min element and find min again for second smallest

PROGRAM

Method 1 - Using Sorting:

# Find the largest/smallest number in a list/tuple
lst = [12, 34, 43, 67, 33, 3, 10]
length = len(lst)
lst.sort()

print("Largest element is:", lst[length-1])
print("Smallest element is:", lst[0])
print("Second Largest element is:", lst[length-2])
print("Second smallest element is:", lst[1])

Method 2 - Using Built-in Functions:

# Alternative method using max() and min() functions
lst = [12, 34, 43, 67, 33, 3, 10]
print("Original List:", lst)

# Find largest and smallest
largest = max(lst)
smallest = min(lst)

print("Largest element is:", largest)
print("Smallest element is:", smallest)

# Find second largest and second smallest
lst_copy = lst.copy()
lst_copy.remove(largest)
second_largest = max(lst_copy)

lst_copy2 = lst.copy()
lst_copy2.remove(smallest)
second_smallest = min(lst_copy2)

print("Second Largest element is:", second_largest)
print("Second smallest element is:", second_smallest)

Method 3 - Working with Tuples:

# Working with tuples
tup = (12, 34, 43, 67, 33, 3, 10)
print("Original Tuple:", tup)

# Convert tuple to list for sorting
lst = list(tup)
lst.sort()

print("Largest element is:", lst[-1])
print("Smallest element is:", lst[0])
print("Second Largest element is:", lst[-2])
print("Second smallest element is:", lst[1])

OUTPUT

Method 1 Output:

Largest element is: 67

Smallest element is: 3

Second Largest element is: 43

Second smallest element is: 10

Method 2 Output:

Original List: [12, 34, 43, 67, 33, 3, 10]

Largest element is: 67

Smallest element is: 3

Second Largest element is: 43

Second smallest element is: 10

Method 3 Output (Tuple):

Original Tuple: (12, 34, 43, 67, 33, 3, 10)

Largest element is: 67

Smallest element is: 3

Second Largest element is: 43

Second smallest element is: 10

CONCLUSION

Thus, the given program was successfully executed and the output was verified as per the expected result.

VIVA QUESTIONS

  1. What is the difference between sort() and sorted() in Python?

    sort() modifies the original list in-place and returns None, while sorted() returns a new sorted list without modifying the original.

  2. How do negative indices work in Python lists?

    Negative indices count from the end of the list. -1 refers to the last element, -2 to the second last, and so on.

  3. What happens if the list has duplicate maximum values?

    max() returns one instance of the maximum value. For second largest, you need to remove all instances of the maximum value.

  4. Can you use these methods on tuples directly?

    max() and min() work directly on tuples, but sort() doesn't. You need to convert tuple to list first for sorting.

  5. What is the time complexity of the sorting approach?

    The sorting approach has O(n log n) time complexity, while the max/min approach has O(n) time complexity.

  6. How would you handle an empty list?

    Check if the list is empty using len(lst) == 0 or if not lst before applying max/min functions to avoid ValueError.

  7. What if the list has only one element?

    For a single element list, largest and smallest are the same. Second largest and smallest would cause IndexError.

  8. How can you find the third largest element?

    After sorting, use lst[-3] for third largest, or remove the two largest elements and find max of remaining elements.

Related Resources

List Tips

  • • Use sort() for in-place sorting
  • • Negative indices count from end
  • • max()/min() work on any iterable
  • • Handle empty lists to avoid errors

Practice More

Try more list manipulation programs to master data structures.

View All Programs →