a = [1,2,2,3,4]
# i = 1
# j = 0
# for i in range(len(a)):
#     if a[i] != a[j]:
#         j += 1
#         a[j] = a[i]
# print(a[:j+1])

for i in range(len(a)-2,-1,-1):
    if a[i] == a[i+1]:
        for j in range(i+1, len(a)):
           # print("a[j-1]: ", a[j-1])
           # print("a[j]: ",a[j])
            a[j-1], a[j] = a[j], a[j-1]
           # print("current a: ", a)
print(a)