you can use:
for i in range(5):
b, c = map(int, input().split())
b_index = a.index(b)
c_index = a.index(c)
a[b_index], a[c_index] = a[c_index], a[b_index]
here is a piece of code to understand the behavior:
a = [1, 2, 3, 4, 5]
def get_index(i, s):
idx = a.index(i)
print(s, i, idx)
return idx
a[get_index(1, 'left')], a[get_index(2, 'left')] = a[get_index(2, 'right')], a[get_index(1, 'right')]
print(a)
output:
right 2 1
[1, 2, 3, 4, 5]
right 1 0
[1, 2, 3, 4, 5]
left 1 0
[1, 2, 3, 4, 5]
left 2 0
[2, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
as you can see get_index(2, 'left') is not the same with get_index(2, 'right'), this shows that a.index(c) doesn't have the same value in the left and in the right side of the = operator
back to your code:
a[a.index(b)], a[a.index(c)] = a[a.index(c)], a[a.index(b)]
first, it is executed the code from the right side of = operator so you have (c, b), then a[a.index(b)] will take the value c, at this step you have 2 of c in your list a, when a.index(c) it is evaluated will return the first c found from the left to right, in the final step you want to set value b to where c was in the beginning, but now you have 2 of c, if c < b you will end with the desired output because the method list.index will return the position of actual c; if b < c then b will take the place where b was in the beginning and this will result in no change in your list a
example:
case b < c
a = [1, 2, 3, 4, 5] --> swap 2 with 5; b = 2, c = 5
a[a.index(b)], a[a.index(c)] = 5, 2
a[a.index(b)] = 5 --> a = [1, 5, 3, 4, 5]
--> a.index(5) --> --> ↑
↑
a[a.index(c)] = 2 --> a = [1, 2, 3, 4, 5]
case b > c
a = [1, 2, 3, 4, 5] --> swap 2 with 5; b = 5, c = 2
a[a.index(b)], a[a.index(c)] = 2, 5
a[a.index(b)] = 2 --> a = [1, 2, 3, 4, 2]
--> a.index(2) --> --> ↑
↑
a[a.index(c)] = 5 --> a = [1, 5, 3, 4, 2]
input(), can you hardcode a known input that fails?a[a.index(1)], a[a.index(2)] = a[a.index(2)], a[a.index(1)]— you don't need all theinput()business — and then simply ask why it is not swapping elements as you might expect.