Swap first and last element Dry Run in PYTHON

Swap first and last element 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.

Swap first and last element Program Code

# Swap first and last element
my_list = [10, 20, 30, 40, 50]
print("Original:", my_list)

# Method 1: Using temporary variable
temp = my_list[0]
my_list[0] = my_list[-1]
my_list[-1] = temp
print("After swap (temp):", my_list)

# Method 2: Tuple unpacking
my_list2 = [10, 20, 30, 40, 50]
my_list2[0], my_list2[-1] = my_list2[-1], my_list2[0]
print("After swap (unpacking):", my_list2)

View the complete Lists programs page.

Program Console Swap first and last element Topic: PYTHON Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.