Swapping elements in a list can be achieved by utilizing multiple assignment and indexing. Here are two examples:
Example 1:
numbers = [1, 2, 3, 4, 5] numbers[0], numbers[1] = numbers[1], numbers[0] print(numbers)
Output:
[2, 1, 3, 4, 5]
Example 2:
fruits = ['apple', 'banana', 'orange'] fruits[1], fruits[2] = fruits[2], fruits[1] print(fruits)
Output:
[‘apple’, ‘orange’, ‘banana’]