-2
a=[1,2,3]
result=[]
for i in range(len(a)):
    x=a[i]
    y=a[:i]+a[i+1:]
    tuple(y)
    for j in range(len(a)):
        list(y)
        y.insert(j,x)
        result.append(y)
        print(y)
        y.remove(x)
print(result)

[1, 2, 3]

[2, 1, 3]

[2, 3, 1]

[2, 1, 3]

[1, 2, 3]

[1, 3, 2]

[3, 1, 2]

[1, 3, 2]

[1, 2, 3]

[[2, 3], [2, 3], [2, 3], [1, 3], [1, 3], [1, 3], [1, 2], [1, 2], [1, 2]]

Process finished with exit code 0

  • 1
    As a statement, `tuple(y)` serves no purpose. It creates a tuple from `y`, then discards it. Either assign it to something (if you need it), or delete the statement. Similarly for `list(y)`, which creates a list and then discards it. Neither statement has any effect other than to clutter your code and slow it down. – Tom Karzes Mar 13 '23 at 07:34
  • Your code is doing exactly what you told it to do: You append `y`, then after appending it, you remove `x` from it. Since the list in `result` still refers to `y`, this modifies the list in `result`. So the first three values have `1` removed, the second three values have `2` removed, and the last three values have `3` removed. – Tom Karzes Mar 13 '23 at 07:40
  • Does this answer your question? [How do I clone a list so that it doesn't change unexpectedly after assignment?](https://stackoverflow.com/questions/2612802/how-do-i-clone-a-list-so-that-it-doesnt-change-unexpectedly-after-assignment) – Tom Karzes Mar 13 '23 at 07:42

1 Answers1

0

To append 2 list you need to use + sign

l1 = [1,2,3]
l2 = [4,5,6]
l3 = l1 + l2

O/P:

l3 = [1,2,3,4,5,6]