As we’ve already seen, tuples are very useful when unpacking them into several variables:
person = ('Mary', 20, 180) name, age, height = person print(name) # Mary print(age) # 20 print(height) # 180
This is very useful when swapping the values of variables:
n1 = 'Anna' n2 = 'Mary' n1, n2 = n2, n1 print(n1) # Mary print(n2) # Anna
Given a list of
nnumbers, you are asked to perform
ooperations on those. Each operation represents a swapping of two numbers. Given 2 indices, the values situated at those locations need to be replaced by one another. You are asked to print the resulting list at the end.
The first line of the input contains two integers
o- the number of elements in the list and the number of operations you need to perform. The next line contains
nintegers separated by a space. The next
olines contain two integers on each line separated by a space - the indices which need to be swapped.
The program should print the resulting list of numbers separated by a space.
Input 5 2 5 6 7 8 9 0 2 1 3 Output 7 8 5 6 9
- Initially -
5 6 7 8 9
- After the first operation -
7 6 5 8 9
- After the second one -
7 8 5 6 9