Can someone explain how this loops through a dictionary?Dictionary Comprehension in Python 3How to merge two dictionaries in a single expression?Can someone explain __all__ in Python?How do I sort a list of dictionaries by a value of the dictionary?How can I safely create a nested directory?How do I return multiple values from a function?How can I make a time delay in Python?How do I sort a dictionary by value?“Least Astonishment” and the Mutable Default ArgumentIterating over dictionaries using 'for' loopsHow to remove a key from a Python dictionary?

Where do I keep track of sorcery points on a character sheet?

Using Forstner bits instead of hole saws

How long should I wait to plug in my refrigerator after unplugging it?

Speaker impedance: rewiring four 8 Ω speakers for use with 8 Ω amp output

Is this popular optical illusion made of a grey-scale image with coloured lines?

How to understand "...to hide the evidence of mishandled magic, or else hidden by castle-proud house-elves" in this sentence

Why is the Vasa Museum in Stockholm so Popular?

Is it moral to remove/hide certain parts of a photo, as a photographer?

Declaring a visitor to the UK as my "girlfriend" - effect on getting a Visitor visa?

Difference between "jail" and "prison" in German

What is the reason behind water not falling from a bucket at the top of loop?

Subverting the essence of fictional and/or religious entities; is it acceptable?

(7 of 11: Fillomino) What is Pyramid Cult's Favorite Shape?

Why does BezierFunction not follow BezierCurve at npts>4?

Does the problem of P vs NP come under the category of Operational Research?

Gold Battle KoTH

Is the "muddled thoughts" from Synaptic Static a magical effect?

Astable 555 circuit not oscillating

Why did the United States not resort to nuclear weapons in Vietnam?

What is Albrecht Dürer's Perspective Machine drawing style?

Accurately recalling the key - can everyone do it?

What does "autolyco-sentimental" mean?

Any information about the photo with Army Uniforms

Is an "are" omitted in this sentence



Can someone explain how this loops through a dictionary?


Dictionary Comprehension in Python 3How to merge two dictionaries in a single expression?Can someone explain __all__ in Python?How do I sort a list of dictionaries by a value of the dictionary?How can I safely create a nested directory?How do I return multiple values from a function?How can I make a time delay in Python?How do I sort a dictionary by value?“Least Astonishment” and the Mutable Default ArgumentIterating over dictionaries using 'for' loopsHow to remove a key from a Python dictionary?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I have two dictionaries, and I want to compare them and see what is different between the two. Where I am getting confused is the dict. Is there a name for this?



Everything is working fine, I just don't really understand why it works or what it is doing.



x = "#04": 0, "#05": 0, "#07": 0, "#08": 1, "#09": 0, "#10": 0, "#11": 1, "#12": 1, "#14": 1, "#15": 1, "#17": 0, "#18": 1, "#19": 1, "#20": 1

y = "#04": 1, "#05": 0, "#07": 0, "#08": 1, "#09": 0, "#10": 0, "#11": 1, "#12": 1, "#14": 1, "#15": 0, "#17": 1, "#18": 1, "#19": 0, "#20": 1

dict = k: x[k] for k in x if y[k] != x[k]

list = []

for k, v in dict.items()
if v==0:
difference = k + ' became ' + '0'
list.append(difference)
else:
difference = k + ' became ' + '1'
list.append(difference)

print(list)


It should print ['#04 became 0', '#15 became 1', '#17 became 0', '#19 became 1'] but I don't understand how the dict works to loop through the x and y dictionaries.










share|improve this question





















  • 3





    It is called "dictionary comprehension", look it up. Also don't use variable names such as dict and list as they will mask the built-in names.

    – Selcuk
    Mar 27 at 1:30






  • 1





    dict is a built-in, you should not use it as a variable.

    – dcg
    Mar 27 at 1:30











  • Possible duplicate of Dictionary Comprehension in Python 3

    – AkshayNevrekar
    Mar 27 at 4:38

















2















I have two dictionaries, and I want to compare them and see what is different between the two. Where I am getting confused is the dict. Is there a name for this?



Everything is working fine, I just don't really understand why it works or what it is doing.



x = "#04": 0, "#05": 0, "#07": 0, "#08": 1, "#09": 0, "#10": 0, "#11": 1, "#12": 1, "#14": 1, "#15": 1, "#17": 0, "#18": 1, "#19": 1, "#20": 1

y = "#04": 1, "#05": 0, "#07": 0, "#08": 1, "#09": 0, "#10": 0, "#11": 1, "#12": 1, "#14": 1, "#15": 0, "#17": 1, "#18": 1, "#19": 0, "#20": 1

dict = k: x[k] for k in x if y[k] != x[k]

list = []

for k, v in dict.items()
if v==0:
difference = k + ' became ' + '0'
list.append(difference)
else:
difference = k + ' became ' + '1'
list.append(difference)

print(list)


It should print ['#04 became 0', '#15 became 1', '#17 became 0', '#19 became 1'] but I don't understand how the dict works to loop through the x and y dictionaries.










share|improve this question





















  • 3





    It is called "dictionary comprehension", look it up. Also don't use variable names such as dict and list as they will mask the built-in names.

    – Selcuk
    Mar 27 at 1:30






  • 1





    dict is a built-in, you should not use it as a variable.

    – dcg
    Mar 27 at 1:30











  • Possible duplicate of Dictionary Comprehension in Python 3

    – AkshayNevrekar
    Mar 27 at 4:38













2












2








2








I have two dictionaries, and I want to compare them and see what is different between the two. Where I am getting confused is the dict. Is there a name for this?



Everything is working fine, I just don't really understand why it works or what it is doing.



x = "#04": 0, "#05": 0, "#07": 0, "#08": 1, "#09": 0, "#10": 0, "#11": 1, "#12": 1, "#14": 1, "#15": 1, "#17": 0, "#18": 1, "#19": 1, "#20": 1

y = "#04": 1, "#05": 0, "#07": 0, "#08": 1, "#09": 0, "#10": 0, "#11": 1, "#12": 1, "#14": 1, "#15": 0, "#17": 1, "#18": 1, "#19": 0, "#20": 1

dict = k: x[k] for k in x if y[k] != x[k]

list = []

for k, v in dict.items()
if v==0:
difference = k + ' became ' + '0'
list.append(difference)
else:
difference = k + ' became ' + '1'
list.append(difference)

print(list)


It should print ['#04 became 0', '#15 became 1', '#17 became 0', '#19 became 1'] but I don't understand how the dict works to loop through the x and y dictionaries.










share|improve this question
















I have two dictionaries, and I want to compare them and see what is different between the two. Where I am getting confused is the dict. Is there a name for this?



Everything is working fine, I just don't really understand why it works or what it is doing.



x = "#04": 0, "#05": 0, "#07": 0, "#08": 1, "#09": 0, "#10": 0, "#11": 1, "#12": 1, "#14": 1, "#15": 1, "#17": 0, "#18": 1, "#19": 1, "#20": 1

y = "#04": 1, "#05": 0, "#07": 0, "#08": 1, "#09": 0, "#10": 0, "#11": 1, "#12": 1, "#14": 1, "#15": 0, "#17": 1, "#18": 1, "#19": 0, "#20": 1

dict = k: x[k] for k in x if y[k] != x[k]

list = []

for k, v in dict.items()
if v==0:
difference = k + ' became ' + '0'
list.append(difference)
else:
difference = k + ' became ' + '1'
list.append(difference)

print(list)


It should print ['#04 became 0', '#15 became 1', '#17 became 0', '#19 became 1'] but I don't understand how the dict works to loop through the x and y dictionaries.







python dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 4:33









Life is complex

8776 silver badges18 bronze badges




8776 silver badges18 bronze badges










asked Mar 27 at 1:27









chummysparrowchummysparrow

132 bronze badges




132 bronze badges










  • 3





    It is called "dictionary comprehension", look it up. Also don't use variable names such as dict and list as they will mask the built-in names.

    – Selcuk
    Mar 27 at 1:30






  • 1





    dict is a built-in, you should not use it as a variable.

    – dcg
    Mar 27 at 1:30











  • Possible duplicate of Dictionary Comprehension in Python 3

    – AkshayNevrekar
    Mar 27 at 4:38












  • 3





    It is called "dictionary comprehension", look it up. Also don't use variable names such as dict and list as they will mask the built-in names.

    – Selcuk
    Mar 27 at 1:30






  • 1





    dict is a built-in, you should not use it as a variable.

    – dcg
    Mar 27 at 1:30











  • Possible duplicate of Dictionary Comprehension in Python 3

    – AkshayNevrekar
    Mar 27 at 4:38







3




3





It is called "dictionary comprehension", look it up. Also don't use variable names such as dict and list as they will mask the built-in names.

– Selcuk
Mar 27 at 1:30





It is called "dictionary comprehension", look it up. Also don't use variable names such as dict and list as they will mask the built-in names.

– Selcuk
Mar 27 at 1:30




1




1





dict is a built-in, you should not use it as a variable.

– dcg
Mar 27 at 1:30





dict is a built-in, you should not use it as a variable.

– dcg
Mar 27 at 1:30













Possible duplicate of Dictionary Comprehension in Python 3

– AkshayNevrekar
Mar 27 at 4:38





Possible duplicate of Dictionary Comprehension in Python 3

– AkshayNevrekar
Mar 27 at 4:38












1 Answer
1






active

oldest

votes


















1














The procedure implemented is comparing two dictionaries assuming that both have the same keys (potentially y could have more entries).



To make this comparison quick, and facilitate the next code block, they decided to generate a dictionary that only contains the keys that have different values.



To generate such dictionary, they use a "dictionary comprehension", which is very efficient.



Now, this construct:



d = k: x[k] for k in x if y[k] != x[k]


can be rewritten as:



d = 
for k,v in x: # for each key->value pairs in dictionary x
if y[k] != x[k]: # if the corresponding elements are different
d[k] = x[k] # store the key->value pair in the new dictionary


You could replace x[k] with v above.






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%2f55368472%2fcan-someone-explain-how-this-loops-through-a-dictionary%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The procedure implemented is comparing two dictionaries assuming that both have the same keys (potentially y could have more entries).



    To make this comparison quick, and facilitate the next code block, they decided to generate a dictionary that only contains the keys that have different values.



    To generate such dictionary, they use a "dictionary comprehension", which is very efficient.



    Now, this construct:



    d = k: x[k] for k in x if y[k] != x[k]


    can be rewritten as:



    d = 
    for k,v in x: # for each key->value pairs in dictionary x
    if y[k] != x[k]: # if the corresponding elements are different
    d[k] = x[k] # store the key->value pair in the new dictionary


    You could replace x[k] with v above.






    share|improve this answer





























      1














      The procedure implemented is comparing two dictionaries assuming that both have the same keys (potentially y could have more entries).



      To make this comparison quick, and facilitate the next code block, they decided to generate a dictionary that only contains the keys that have different values.



      To generate such dictionary, they use a "dictionary comprehension", which is very efficient.



      Now, this construct:



      d = k: x[k] for k in x if y[k] != x[k]


      can be rewritten as:



      d = 
      for k,v in x: # for each key->value pairs in dictionary x
      if y[k] != x[k]: # if the corresponding elements are different
      d[k] = x[k] # store the key->value pair in the new dictionary


      You could replace x[k] with v above.






      share|improve this answer



























        1












        1








        1







        The procedure implemented is comparing two dictionaries assuming that both have the same keys (potentially y could have more entries).



        To make this comparison quick, and facilitate the next code block, they decided to generate a dictionary that only contains the keys that have different values.



        To generate such dictionary, they use a "dictionary comprehension", which is very efficient.



        Now, this construct:



        d = k: x[k] for k in x if y[k] != x[k]


        can be rewritten as:



        d = 
        for k,v in x: # for each key->value pairs in dictionary x
        if y[k] != x[k]: # if the corresponding elements are different
        d[k] = x[k] # store the key->value pair in the new dictionary


        You could replace x[k] with v above.






        share|improve this answer













        The procedure implemented is comparing two dictionaries assuming that both have the same keys (potentially y could have more entries).



        To make this comparison quick, and facilitate the next code block, they decided to generate a dictionary that only contains the keys that have different values.



        To generate such dictionary, they use a "dictionary comprehension", which is very efficient.



        Now, this construct:



        d = k: x[k] for k in x if y[k] != x[k]


        can be rewritten as:



        d = 
        for k,v in x: # for each key->value pairs in dictionary x
        if y[k] != x[k]: # if the corresponding elements are different
        d[k] = x[k] # store the key->value pair in the new dictionary


        You could replace x[k] with v above.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 5:09









        salsal

        2,0091 gold badge5 silver badges15 bronze badges




        2,0091 gold badge5 silver badges15 bronze badges





















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            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%2f55368472%2fcan-someone-explain-how-this-loops-through-a-dictionary%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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해