Things get wrong when appending a list to a list in python












1














I am programming a simple software for practicing.



def check(num, lst):
for i in lst:
if num == i:
return False
else:
lst.append(num)
return True

timelst =
for i1 in range(1,7):
usednum = [i1]

for i2 in range(1,7):
if not check(i2, usednum):
continue

for i3 in range(1,7):
if not check(i3, usednum):
continue

for i4 in range(1,7):
if not check(i4, usednum):
continue

for i5 in range(1,7):
if not check(i5, usednum):
continue

for i6 in range(1,7):
if not check(i6, usednum):
continue
else:

print(usednum) #print the appending list before actual appending
timelst.append(usednum)
usednum.pop()
break

usednum.pop()


When running this, this is what I want the timelst to be like:



[[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 6, 5],
[2, 1, 3, 4, 5, 6],
[2, 1, 3, 4, 6, 5],
...
]


However this is what I actually get:



[[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[2, 1, 3, 4, 5, 6],
[2, 1, 3, 4, 5, 6],
...
]


I am really confused about this and got stuck for a long time. I tried printing the 'usednum' list before appending it, and it returns perfectly what I want.



[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 6, 5]
[2, 1, 3, 4, 5, 6]
[2, 1, 3, 4, 6, 5]
#printed lists while running the program


But the same problem still occurs everytime when I check the timelst after executing the program.



I used python 3.7 with spyder. There should not be a problem with my compiler, as I have tried running this on ipython and I still got the same result.



Can anyone please help me solving this problem? Thanks!










share|improve this question




















  • 1




    Multiple nested loops like this is generally considered bad programming practice, especially when the loops do the same thing. It's not entirely clear to me what you want the output to be ... what is the intention of this code?
    – Jonah Bishop
    Nov 19 at 22:04










  • Basically I want to append every possible combinations of the numbers under six to a list, such as 123456 123465 123546 123564 ... 654312 654321, and this is the most efficient way I can think to achieve this, rather than append all possible lists and remove the ones with repeating numbers. The program is not totally finished, it should work, ideally and maybe logically, if I add timelst.pop() at the end of every loop after the previous loop finishes. I did try it, but it gave me a much worse result like [[1], [1], [1], ...., [6], [6], [6]]. I realise maybe they are caused by the same problem.
    – PotatoPoweredLaptop
    Nov 19 at 22:30










  • check the indentation of your check function.
    – Paul H
    Nov 19 at 22:56










  • Sorry that was just a silly mistake when I was transferring my code to my post. It was correct in my compiler and that did not cause any problem, and the indentation of function has been corrected in my post
    – PotatoPoweredLaptop
    Nov 19 at 23:20


















1














I am programming a simple software for practicing.



def check(num, lst):
for i in lst:
if num == i:
return False
else:
lst.append(num)
return True

timelst =
for i1 in range(1,7):
usednum = [i1]

for i2 in range(1,7):
if not check(i2, usednum):
continue

for i3 in range(1,7):
if not check(i3, usednum):
continue

for i4 in range(1,7):
if not check(i4, usednum):
continue

for i5 in range(1,7):
if not check(i5, usednum):
continue

for i6 in range(1,7):
if not check(i6, usednum):
continue
else:

print(usednum) #print the appending list before actual appending
timelst.append(usednum)
usednum.pop()
break

usednum.pop()


When running this, this is what I want the timelst to be like:



[[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 6, 5],
[2, 1, 3, 4, 5, 6],
[2, 1, 3, 4, 6, 5],
...
]


However this is what I actually get:



[[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[2, 1, 3, 4, 5, 6],
[2, 1, 3, 4, 5, 6],
...
]


I am really confused about this and got stuck for a long time. I tried printing the 'usednum' list before appending it, and it returns perfectly what I want.



[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 6, 5]
[2, 1, 3, 4, 5, 6]
[2, 1, 3, 4, 6, 5]
#printed lists while running the program


But the same problem still occurs everytime when I check the timelst after executing the program.



I used python 3.7 with spyder. There should not be a problem with my compiler, as I have tried running this on ipython and I still got the same result.



Can anyone please help me solving this problem? Thanks!










share|improve this question




















  • 1




    Multiple nested loops like this is generally considered bad programming practice, especially when the loops do the same thing. It's not entirely clear to me what you want the output to be ... what is the intention of this code?
    – Jonah Bishop
    Nov 19 at 22:04










  • Basically I want to append every possible combinations of the numbers under six to a list, such as 123456 123465 123546 123564 ... 654312 654321, and this is the most efficient way I can think to achieve this, rather than append all possible lists and remove the ones with repeating numbers. The program is not totally finished, it should work, ideally and maybe logically, if I add timelst.pop() at the end of every loop after the previous loop finishes. I did try it, but it gave me a much worse result like [[1], [1], [1], ...., [6], [6], [6]]. I realise maybe they are caused by the same problem.
    – PotatoPoweredLaptop
    Nov 19 at 22:30










  • check the indentation of your check function.
    – Paul H
    Nov 19 at 22:56










  • Sorry that was just a silly mistake when I was transferring my code to my post. It was correct in my compiler and that did not cause any problem, and the indentation of function has been corrected in my post
    – PotatoPoweredLaptop
    Nov 19 at 23:20
















1












1








1


0





I am programming a simple software for practicing.



def check(num, lst):
for i in lst:
if num == i:
return False
else:
lst.append(num)
return True

timelst =
for i1 in range(1,7):
usednum = [i1]

for i2 in range(1,7):
if not check(i2, usednum):
continue

for i3 in range(1,7):
if not check(i3, usednum):
continue

for i4 in range(1,7):
if not check(i4, usednum):
continue

for i5 in range(1,7):
if not check(i5, usednum):
continue

for i6 in range(1,7):
if not check(i6, usednum):
continue
else:

print(usednum) #print the appending list before actual appending
timelst.append(usednum)
usednum.pop()
break

usednum.pop()


When running this, this is what I want the timelst to be like:



[[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 6, 5],
[2, 1, 3, 4, 5, 6],
[2, 1, 3, 4, 6, 5],
...
]


However this is what I actually get:



[[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[2, 1, 3, 4, 5, 6],
[2, 1, 3, 4, 5, 6],
...
]


I am really confused about this and got stuck for a long time. I tried printing the 'usednum' list before appending it, and it returns perfectly what I want.



[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 6, 5]
[2, 1, 3, 4, 5, 6]
[2, 1, 3, 4, 6, 5]
#printed lists while running the program


But the same problem still occurs everytime when I check the timelst after executing the program.



I used python 3.7 with spyder. There should not be a problem with my compiler, as I have tried running this on ipython and I still got the same result.



Can anyone please help me solving this problem? Thanks!










share|improve this question















I am programming a simple software for practicing.



def check(num, lst):
for i in lst:
if num == i:
return False
else:
lst.append(num)
return True

timelst =
for i1 in range(1,7):
usednum = [i1]

for i2 in range(1,7):
if not check(i2, usednum):
continue

for i3 in range(1,7):
if not check(i3, usednum):
continue

for i4 in range(1,7):
if not check(i4, usednum):
continue

for i5 in range(1,7):
if not check(i5, usednum):
continue

for i6 in range(1,7):
if not check(i6, usednum):
continue
else:

print(usednum) #print the appending list before actual appending
timelst.append(usednum)
usednum.pop()
break

usednum.pop()


When running this, this is what I want the timelst to be like:



[[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 6, 5],
[2, 1, 3, 4, 5, 6],
[2, 1, 3, 4, 6, 5],
...
]


However this is what I actually get:



[[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[2, 1, 3, 4, 5, 6],
[2, 1, 3, 4, 5, 6],
...
]


I am really confused about this and got stuck for a long time. I tried printing the 'usednum' list before appending it, and it returns perfectly what I want.



[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 6, 5]
[2, 1, 3, 4, 5, 6]
[2, 1, 3, 4, 6, 5]
#printed lists while running the program


But the same problem still occurs everytime when I check the timelst after executing the program.



I used python 3.7 with spyder. There should not be a problem with my compiler, as I have tried running this on ipython and I still got the same result.



Can anyone please help me solving this problem? Thanks!







python list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 23:11

























asked Nov 19 at 21:42









PotatoPoweredLaptop

64




64








  • 1




    Multiple nested loops like this is generally considered bad programming practice, especially when the loops do the same thing. It's not entirely clear to me what you want the output to be ... what is the intention of this code?
    – Jonah Bishop
    Nov 19 at 22:04










  • Basically I want to append every possible combinations of the numbers under six to a list, such as 123456 123465 123546 123564 ... 654312 654321, and this is the most efficient way I can think to achieve this, rather than append all possible lists and remove the ones with repeating numbers. The program is not totally finished, it should work, ideally and maybe logically, if I add timelst.pop() at the end of every loop after the previous loop finishes. I did try it, but it gave me a much worse result like [[1], [1], [1], ...., [6], [6], [6]]. I realise maybe they are caused by the same problem.
    – PotatoPoweredLaptop
    Nov 19 at 22:30










  • check the indentation of your check function.
    – Paul H
    Nov 19 at 22:56










  • Sorry that was just a silly mistake when I was transferring my code to my post. It was correct in my compiler and that did not cause any problem, and the indentation of function has been corrected in my post
    – PotatoPoweredLaptop
    Nov 19 at 23:20
















  • 1




    Multiple nested loops like this is generally considered bad programming practice, especially when the loops do the same thing. It's not entirely clear to me what you want the output to be ... what is the intention of this code?
    – Jonah Bishop
    Nov 19 at 22:04










  • Basically I want to append every possible combinations of the numbers under six to a list, such as 123456 123465 123546 123564 ... 654312 654321, and this is the most efficient way I can think to achieve this, rather than append all possible lists and remove the ones with repeating numbers. The program is not totally finished, it should work, ideally and maybe logically, if I add timelst.pop() at the end of every loop after the previous loop finishes. I did try it, but it gave me a much worse result like [[1], [1], [1], ...., [6], [6], [6]]. I realise maybe they are caused by the same problem.
    – PotatoPoweredLaptop
    Nov 19 at 22:30










  • check the indentation of your check function.
    – Paul H
    Nov 19 at 22:56










  • Sorry that was just a silly mistake when I was transferring my code to my post. It was correct in my compiler and that did not cause any problem, and the indentation of function has been corrected in my post
    – PotatoPoweredLaptop
    Nov 19 at 23:20










1




1




Multiple nested loops like this is generally considered bad programming practice, especially when the loops do the same thing. It's not entirely clear to me what you want the output to be ... what is the intention of this code?
– Jonah Bishop
Nov 19 at 22:04




Multiple nested loops like this is generally considered bad programming practice, especially when the loops do the same thing. It's not entirely clear to me what you want the output to be ... what is the intention of this code?
– Jonah Bishop
Nov 19 at 22:04












Basically I want to append every possible combinations of the numbers under six to a list, such as 123456 123465 123546 123564 ... 654312 654321, and this is the most efficient way I can think to achieve this, rather than append all possible lists and remove the ones with repeating numbers. The program is not totally finished, it should work, ideally and maybe logically, if I add timelst.pop() at the end of every loop after the previous loop finishes. I did try it, but it gave me a much worse result like [[1], [1], [1], ...., [6], [6], [6]]. I realise maybe they are caused by the same problem.
– PotatoPoweredLaptop
Nov 19 at 22:30




Basically I want to append every possible combinations of the numbers under six to a list, such as 123456 123465 123546 123564 ... 654312 654321, and this is the most efficient way I can think to achieve this, rather than append all possible lists and remove the ones with repeating numbers. The program is not totally finished, it should work, ideally and maybe logically, if I add timelst.pop() at the end of every loop after the previous loop finishes. I did try it, but it gave me a much worse result like [[1], [1], [1], ...., [6], [6], [6]]. I realise maybe they are caused by the same problem.
– PotatoPoweredLaptop
Nov 19 at 22:30












check the indentation of your check function.
– Paul H
Nov 19 at 22:56




check the indentation of your check function.
– Paul H
Nov 19 at 22:56












Sorry that was just a silly mistake when I was transferring my code to my post. It was correct in my compiler and that did not cause any problem, and the indentation of function has been corrected in my post
– PotatoPoweredLaptop
Nov 19 at 23:20






Sorry that was just a silly mistake when I was transferring my code to my post. It was correct in my compiler and that did not cause any problem, and the indentation of function has been corrected in my post
– PotatoPoweredLaptop
Nov 19 at 23:20














2 Answers
2






active

oldest

votes


















0














You can make your life a lot easier by using the itertools.permutations() function call:



import itertools

mylist = [1, 2, 3, 4, 5, 6]
for item in itertools.permutations(mylist):
# Do something with item, which is a permutation of 'mylist'
print(item)


Note, however, that this results in a big (6! ... that is six-factorial) list of results. If you only want combinations, take a look at itertools.combinations() instead.






share|improve this answer





















  • Thank you very much! I didn't realise I could find a toolkit to reach my purpose. However, at this stage, I am still wondering what makes my program not work properly, as the 'usednum' list printed in the previous step is [1, 2, 3, 4, 6, 5], but as it is appended to the 'timelst', it becomes [1, 2, 3, 4, 5, 6].
    – PotatoPoweredLaptop
    Nov 19 at 23:17



















0














The problem has been solved! I should have realised earlier that there had been an issue about the shallow and deep copy of the list I have appended. I have add



usednum = copy.deepcopy(usednum)


to the final loop, so it now looks like



for i6 in range(1, 7):
if not check(i6, usednum):
continue
else:
timelst.append(usednum)
usednum = copy.deepcopy(usednum) #the new copy of the appended list, so now there's nothing to do with the appended one.
print('i6',usednum.pop())
break

usednum.pop()


which can work properly now






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383053%2fthings-get-wrong-when-appending-a-list-to-a-list-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can make your life a lot easier by using the itertools.permutations() function call:



    import itertools

    mylist = [1, 2, 3, 4, 5, 6]
    for item in itertools.permutations(mylist):
    # Do something with item, which is a permutation of 'mylist'
    print(item)


    Note, however, that this results in a big (6! ... that is six-factorial) list of results. If you only want combinations, take a look at itertools.combinations() instead.






    share|improve this answer





















    • Thank you very much! I didn't realise I could find a toolkit to reach my purpose. However, at this stage, I am still wondering what makes my program not work properly, as the 'usednum' list printed in the previous step is [1, 2, 3, 4, 6, 5], but as it is appended to the 'timelst', it becomes [1, 2, 3, 4, 5, 6].
      – PotatoPoweredLaptop
      Nov 19 at 23:17
















    0














    You can make your life a lot easier by using the itertools.permutations() function call:



    import itertools

    mylist = [1, 2, 3, 4, 5, 6]
    for item in itertools.permutations(mylist):
    # Do something with item, which is a permutation of 'mylist'
    print(item)


    Note, however, that this results in a big (6! ... that is six-factorial) list of results. If you only want combinations, take a look at itertools.combinations() instead.






    share|improve this answer





















    • Thank you very much! I didn't realise I could find a toolkit to reach my purpose. However, at this stage, I am still wondering what makes my program not work properly, as the 'usednum' list printed in the previous step is [1, 2, 3, 4, 6, 5], but as it is appended to the 'timelst', it becomes [1, 2, 3, 4, 5, 6].
      – PotatoPoweredLaptop
      Nov 19 at 23:17














    0












    0








    0






    You can make your life a lot easier by using the itertools.permutations() function call:



    import itertools

    mylist = [1, 2, 3, 4, 5, 6]
    for item in itertools.permutations(mylist):
    # Do something with item, which is a permutation of 'mylist'
    print(item)


    Note, however, that this results in a big (6! ... that is six-factorial) list of results. If you only want combinations, take a look at itertools.combinations() instead.






    share|improve this answer












    You can make your life a lot easier by using the itertools.permutations() function call:



    import itertools

    mylist = [1, 2, 3, 4, 5, 6]
    for item in itertools.permutations(mylist):
    # Do something with item, which is a permutation of 'mylist'
    print(item)


    Note, however, that this results in a big (6! ... that is six-factorial) list of results. If you only want combinations, take a look at itertools.combinations() instead.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 at 22:55









    Jonah Bishop

    8,26732957




    8,26732957












    • Thank you very much! I didn't realise I could find a toolkit to reach my purpose. However, at this stage, I am still wondering what makes my program not work properly, as the 'usednum' list printed in the previous step is [1, 2, 3, 4, 6, 5], but as it is appended to the 'timelst', it becomes [1, 2, 3, 4, 5, 6].
      – PotatoPoweredLaptop
      Nov 19 at 23:17


















    • Thank you very much! I didn't realise I could find a toolkit to reach my purpose. However, at this stage, I am still wondering what makes my program not work properly, as the 'usednum' list printed in the previous step is [1, 2, 3, 4, 6, 5], but as it is appended to the 'timelst', it becomes [1, 2, 3, 4, 5, 6].
      – PotatoPoweredLaptop
      Nov 19 at 23:17
















    Thank you very much! I didn't realise I could find a toolkit to reach my purpose. However, at this stage, I am still wondering what makes my program not work properly, as the 'usednum' list printed in the previous step is [1, 2, 3, 4, 6, 5], but as it is appended to the 'timelst', it becomes [1, 2, 3, 4, 5, 6].
    – PotatoPoweredLaptop
    Nov 19 at 23:17




    Thank you very much! I didn't realise I could find a toolkit to reach my purpose. However, at this stage, I am still wondering what makes my program not work properly, as the 'usednum' list printed in the previous step is [1, 2, 3, 4, 6, 5], but as it is appended to the 'timelst', it becomes [1, 2, 3, 4, 5, 6].
    – PotatoPoweredLaptop
    Nov 19 at 23:17













    0














    The problem has been solved! I should have realised earlier that there had been an issue about the shallow and deep copy of the list I have appended. I have add



    usednum = copy.deepcopy(usednum)


    to the final loop, so it now looks like



    for i6 in range(1, 7):
    if not check(i6, usednum):
    continue
    else:
    timelst.append(usednum)
    usednum = copy.deepcopy(usednum) #the new copy of the appended list, so now there's nothing to do with the appended one.
    print('i6',usednum.pop())
    break

    usednum.pop()


    which can work properly now






    share|improve this answer


























      0














      The problem has been solved! I should have realised earlier that there had been an issue about the shallow and deep copy of the list I have appended. I have add



      usednum = copy.deepcopy(usednum)


      to the final loop, so it now looks like



      for i6 in range(1, 7):
      if not check(i6, usednum):
      continue
      else:
      timelst.append(usednum)
      usednum = copy.deepcopy(usednum) #the new copy of the appended list, so now there's nothing to do with the appended one.
      print('i6',usednum.pop())
      break

      usednum.pop()


      which can work properly now






      share|improve this answer
























        0












        0








        0






        The problem has been solved! I should have realised earlier that there had been an issue about the shallow and deep copy of the list I have appended. I have add



        usednum = copy.deepcopy(usednum)


        to the final loop, so it now looks like



        for i6 in range(1, 7):
        if not check(i6, usednum):
        continue
        else:
        timelst.append(usednum)
        usednum = copy.deepcopy(usednum) #the new copy of the appended list, so now there's nothing to do with the appended one.
        print('i6',usednum.pop())
        break

        usednum.pop()


        which can work properly now






        share|improve this answer












        The problem has been solved! I should have realised earlier that there had been an issue about the shallow and deep copy of the list I have appended. I have add



        usednum = copy.deepcopy(usednum)


        to the final loop, so it now looks like



        for i6 in range(1, 7):
        if not check(i6, usednum):
        continue
        else:
        timelst.append(usednum)
        usednum = copy.deepcopy(usednum) #the new copy of the appended list, so now there's nothing to do with the appended one.
        print('i6',usednum.pop())
        break

        usednum.pop()


        which can work properly now







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 23:58









        PotatoPoweredLaptop

        64




        64






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383053%2fthings-get-wrong-when-appending-a-list-to-a-list-in-python%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?