Strange Effect of Python Assignment?












0















As we all know, for Python, we could easily assignment multiple variables in one line. But here I encounter a strange situation. Say we have a list:



x = [1, 2, 3, 4]


And then, we do



x[0], x[x[0]] = 2, 1


Finally, we would get



x = [2, 2, 3, 4]


instead of



x = [2, 1, 3, 4]


Could anyone explain what is going wrong here? How would python implement the multiple variables assignment in one line?



Thanks in advance.










share|improve this question























  • Please also check this: stackoverflow.com/questions/8725673/…

    – supl
    Nov 22 '18 at 5:34











  • So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!

    – UserNotFound
    Nov 22 '18 at 5:42
















0















As we all know, for Python, we could easily assignment multiple variables in one line. But here I encounter a strange situation. Say we have a list:



x = [1, 2, 3, 4]


And then, we do



x[0], x[x[0]] = 2, 1


Finally, we would get



x = [2, 2, 3, 4]


instead of



x = [2, 1, 3, 4]


Could anyone explain what is going wrong here? How would python implement the multiple variables assignment in one line?



Thanks in advance.










share|improve this question























  • Please also check this: stackoverflow.com/questions/8725673/…

    – supl
    Nov 22 '18 at 5:34











  • So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!

    – UserNotFound
    Nov 22 '18 at 5:42














0












0








0








As we all know, for Python, we could easily assignment multiple variables in one line. But here I encounter a strange situation. Say we have a list:



x = [1, 2, 3, 4]


And then, we do



x[0], x[x[0]] = 2, 1


Finally, we would get



x = [2, 2, 3, 4]


instead of



x = [2, 1, 3, 4]


Could anyone explain what is going wrong here? How would python implement the multiple variables assignment in one line?



Thanks in advance.










share|improve this question














As we all know, for Python, we could easily assignment multiple variables in one line. But here I encounter a strange situation. Say we have a list:



x = [1, 2, 3, 4]


And then, we do



x[0], x[x[0]] = 2, 1


Finally, we would get



x = [2, 2, 3, 4]


instead of



x = [2, 1, 3, 4]


Could anyone explain what is going wrong here? How would python implement the multiple variables assignment in one line?



Thanks in advance.







python syntax programming-languages






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 5:21









UserNotFoundUserNotFound

11




11













  • Please also check this: stackoverflow.com/questions/8725673/…

    – supl
    Nov 22 '18 at 5:34











  • So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!

    – UserNotFound
    Nov 22 '18 at 5:42



















  • Please also check this: stackoverflow.com/questions/8725673/…

    – supl
    Nov 22 '18 at 5:34











  • So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!

    – UserNotFound
    Nov 22 '18 at 5:42

















Please also check this: stackoverflow.com/questions/8725673/…

– supl
Nov 22 '18 at 5:34





Please also check this: stackoverflow.com/questions/8725673/…

– supl
Nov 22 '18 at 5:34













So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!

– UserNotFound
Nov 22 '18 at 5:42





So basically, Python will evaluate right-hand side of the statement (only read, no contention), and then it will assign to variables on the left-hand side from left to right. Thanks all!!

– UserNotFound
Nov 22 '18 at 5:42












2 Answers
2






active

oldest

votes


















1














The resulting list is not [2, 2, 3, 4], it's [2, 2, 1, 4]. x[0] is assigned the value 2, then x[x[0]] becomes x[2] and is assigned the value 1






share|improve this answer
























  • yes. It is [2, 2, 1, 4], Thanks!

    – UserNotFound
    Nov 22 '18 at 5:42



















0














What happens here is, python first executes your first instruction . x[0]= 2



So after 1st execution, x=[2,2,3,4]



Then it executes the 2nd one. So it changes the value of x[2]



after 2nd execution, x=[2,2,1,4]



and gives the result as [2, 2, 1, 4]






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%2f53424334%2fstrange-effect-of-python-assignment%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









    1














    The resulting list is not [2, 2, 3, 4], it's [2, 2, 1, 4]. x[0] is assigned the value 2, then x[x[0]] becomes x[2] and is assigned the value 1






    share|improve this answer
























    • yes. It is [2, 2, 1, 4], Thanks!

      – UserNotFound
      Nov 22 '18 at 5:42
















    1














    The resulting list is not [2, 2, 3, 4], it's [2, 2, 1, 4]. x[0] is assigned the value 2, then x[x[0]] becomes x[2] and is assigned the value 1






    share|improve this answer
























    • yes. It is [2, 2, 1, 4], Thanks!

      – UserNotFound
      Nov 22 '18 at 5:42














    1












    1








    1







    The resulting list is not [2, 2, 3, 4], it's [2, 2, 1, 4]. x[0] is assigned the value 2, then x[x[0]] becomes x[2] and is assigned the value 1






    share|improve this answer













    The resulting list is not [2, 2, 3, 4], it's [2, 2, 1, 4]. x[0] is assigned the value 2, then x[x[0]] becomes x[2] and is assigned the value 1







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 22 '18 at 5:29









    M.GM.G

    388310




    388310













    • yes. It is [2, 2, 1, 4], Thanks!

      – UserNotFound
      Nov 22 '18 at 5:42



















    • yes. It is [2, 2, 1, 4], Thanks!

      – UserNotFound
      Nov 22 '18 at 5:42

















    yes. It is [2, 2, 1, 4], Thanks!

    – UserNotFound
    Nov 22 '18 at 5:42





    yes. It is [2, 2, 1, 4], Thanks!

    – UserNotFound
    Nov 22 '18 at 5:42













    0














    What happens here is, python first executes your first instruction . x[0]= 2



    So after 1st execution, x=[2,2,3,4]



    Then it executes the 2nd one. So it changes the value of x[2]



    after 2nd execution, x=[2,2,1,4]



    and gives the result as [2, 2, 1, 4]






    share|improve this answer




























      0














      What happens here is, python first executes your first instruction . x[0]= 2



      So after 1st execution, x=[2,2,3,4]



      Then it executes the 2nd one. So it changes the value of x[2]



      after 2nd execution, x=[2,2,1,4]



      and gives the result as [2, 2, 1, 4]






      share|improve this answer


























        0












        0








        0







        What happens here is, python first executes your first instruction . x[0]= 2



        So after 1st execution, x=[2,2,3,4]



        Then it executes the 2nd one. So it changes the value of x[2]



        after 2nd execution, x=[2,2,1,4]



        and gives the result as [2, 2, 1, 4]






        share|improve this answer













        What happens here is, python first executes your first instruction . x[0]= 2



        So after 1st execution, x=[2,2,3,4]



        Then it executes the 2nd one. So it changes the value of x[2]



        after 2nd execution, x=[2,2,1,4]



        and gives the result as [2, 2, 1, 4]







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 5:30









        Sandesh34Sandesh34

        254113




        254113






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424334%2fstrange-effect-of-python-assignment%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

            RAC Tourist Trophy