Find second largest number Dry Run in PYTHON

Find second largest number is an interactive PYTHON dry run visualizer from the Lists programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Find second largest number Program Code

# Find second largest element
numbers = [45, 78, 23, 89, 12, 56, 91, 34, 91]
print("List:", numbers)

# Method 1: Using sorting
unique_sorted = sorted(set(numbers))
if len(unique_sorted) >= 2:
    print(f"Second largest (sorting): {unique_sorted[-2]}")

# Method 2: Single pass
largest = second = float('-inf')
for num in numbers:
    if num > largest:
        second = largest
        largest = num
    elif num > second and num != largest:
        second = num

print(f"Second largest (single pass): {second}")

View the complete Lists programs page.

Program Console Find second largest number Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.