Iterating through dictionariesHow to merge two dictionaries in a single expression?How do I efficiently iterate over each entry in a Java Map?How do I sort a list of dictionaries by a value of the dictionary?What is the best way to iterate over a dictionary?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryIterating over dictionaries using 'for' loopsDelete an element from a dictionaryHow to remove a key from a Python dictionary?

Can I make popcorn with any corn?

How much of data wrangling is a data scientist's job?

Can a Cauchy sequence converge for one metric while not converging for another?

How to format long polynomial?

What defenses are there against being summoned by the Gate spell?

What's the point of deactivating Num Lock on login screens?

Character reincarnated...as a snail

What does it mean to describe someone as a butt steak?

How does one intimidate enemies without having the capacity for violence?

Paid for article while in US on F-1 visa?

Arrow those variables!

Modeling an IP Address

Has there ever been an airliner design involving reducing generator load by installing solar panels?

NMaximize is not converging to a solution

LWC SFDX source push error TypeError: LWC1009: decl.moveTo is not a function

Is it inappropriate for a student to attend their mentor's dissertation defense?

Can I ask the recruiters in my resume to put the reason why I am rejected?

infared filters v nd

tikz convert color string to hex value

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?

RSA: Danger of using p to create q

What's that red-plus icon near a text?

Are astronomers waiting to see something in an image from a gravitational lens that they've already seen in an adjacent image?



Iterating through dictionaries


How to merge two dictionaries in a single expression?How do I efficiently iterate over each entry in a Java Map?How do I sort a list of dictionaries by a value of the dictionary?What is the best way to iterate over a dictionary?How do I sort a dictionary by value?Add new keys to a dictionary?Check if a given key already exists in a dictionaryIterating over dictionaries using 'for' loopsDelete an element from a dictionaryHow to remove a key from a Python dictionary?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have a dictionary of 3 lists, each containing 3 elements, and want to alter each element within each list separately, generating 9 new dictionaries as such:



d = 1:[a,b,c], 2:[e,f,g], 3:[h,i,j]
d1 = 1:[a+k,b,c], 2:[e,f,g], 3:[h,i,j]
d2 = 1:[a, b+k, c], 2:[e,f,g], 3:[h,i,j]
...
d9 = {1:[a,b,c], 2:[e,f,g], 3:[h,i,j+k]


Is there a way this can be done without having to hardcode each dictionary separately? Further on down the code, I will need to refer to the sets of lists separately (i.e. I will input [a,b,c+k], [d,e,f], [g,h,i] into some function to get a solution), but I am happy for the data not to be in dictionary form so long as I can still refer to their sets later!










share|improve this question
























  • I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?

    – busybear
    Mar 9 at 1:05











  • Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.

    – Plasmid
    Mar 9 at 1:08











  • Put d1-d9 in a list?

    – wjandrea
    Mar 9 at 1:09











  • @wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9

    – Plasmid
    Mar 9 at 1:11











  • @Plasmid Yes, use a list comprehension.

    – wjandrea
    Mar 9 at 1:11

















1















I have a dictionary of 3 lists, each containing 3 elements, and want to alter each element within each list separately, generating 9 new dictionaries as such:



d = 1:[a,b,c], 2:[e,f,g], 3:[h,i,j]
d1 = 1:[a+k,b,c], 2:[e,f,g], 3:[h,i,j]
d2 = 1:[a, b+k, c], 2:[e,f,g], 3:[h,i,j]
...
d9 = {1:[a,b,c], 2:[e,f,g], 3:[h,i,j+k]


Is there a way this can be done without having to hardcode each dictionary separately? Further on down the code, I will need to refer to the sets of lists separately (i.e. I will input [a,b,c+k], [d,e,f], [g,h,i] into some function to get a solution), but I am happy for the data not to be in dictionary form so long as I can still refer to their sets later!










share|improve this question
























  • I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?

    – busybear
    Mar 9 at 1:05











  • Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.

    – Plasmid
    Mar 9 at 1:08











  • Put d1-d9 in a list?

    – wjandrea
    Mar 9 at 1:09











  • @wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9

    – Plasmid
    Mar 9 at 1:11











  • @Plasmid Yes, use a list comprehension.

    – wjandrea
    Mar 9 at 1:11













1












1








1








I have a dictionary of 3 lists, each containing 3 elements, and want to alter each element within each list separately, generating 9 new dictionaries as such:



d = 1:[a,b,c], 2:[e,f,g], 3:[h,i,j]
d1 = 1:[a+k,b,c], 2:[e,f,g], 3:[h,i,j]
d2 = 1:[a, b+k, c], 2:[e,f,g], 3:[h,i,j]
...
d9 = {1:[a,b,c], 2:[e,f,g], 3:[h,i,j+k]


Is there a way this can be done without having to hardcode each dictionary separately? Further on down the code, I will need to refer to the sets of lists separately (i.e. I will input [a,b,c+k], [d,e,f], [g,h,i] into some function to get a solution), but I am happy for the data not to be in dictionary form so long as I can still refer to their sets later!










share|improve this question
















I have a dictionary of 3 lists, each containing 3 elements, and want to alter each element within each list separately, generating 9 new dictionaries as such:



d = 1:[a,b,c], 2:[e,f,g], 3:[h,i,j]
d1 = 1:[a+k,b,c], 2:[e,f,g], 3:[h,i,j]
d2 = 1:[a, b+k, c], 2:[e,f,g], 3:[h,i,j]
...
d9 = {1:[a,b,c], 2:[e,f,g], 3:[h,i,j+k]


Is there a way this can be done without having to hardcode each dictionary separately? Further on down the code, I will need to refer to the sets of lists separately (i.e. I will input [a,b,c+k], [d,e,f], [g,h,i] into some function to get a solution), but I am happy for the data not to be in dictionary form so long as I can still refer to their sets later!







python dictionary






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 1:07







Plasmid

















asked Mar 9 at 1:02









PlasmidPlasmid

174




174












  • I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?

    – busybear
    Mar 9 at 1:05











  • Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.

    – Plasmid
    Mar 9 at 1:08











  • Put d1-d9 in a list?

    – wjandrea
    Mar 9 at 1:09











  • @wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9

    – Plasmid
    Mar 9 at 1:11











  • @Plasmid Yes, use a list comprehension.

    – wjandrea
    Mar 9 at 1:11

















  • I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?

    – busybear
    Mar 9 at 1:05











  • Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.

    – Plasmid
    Mar 9 at 1:08











  • Put d1-d9 in a list?

    – wjandrea
    Mar 9 at 1:09











  • @wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9

    – Plasmid
    Mar 9 at 1:11











  • @Plasmid Yes, use a list comprehension.

    – wjandrea
    Mar 9 at 1:11
















I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?

– busybear
Mar 9 at 1:05





I'm not too clear on what you are trying to do. What are you starting with and what are you expecting to get?

– busybear
Mar 9 at 1:05













Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.

– Plasmid
Mar 9 at 1:08





Basically, I have three vectors stored in a dictionary, and I need to alter each individual coordinate in each vector separately.

– Plasmid
Mar 9 at 1:08













Put d1-d9 in a list?

– wjandrea
Mar 9 at 1:09





Put d1-d9 in a list?

– wjandrea
Mar 9 at 1:09













@wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9

– Plasmid
Mar 9 at 1:11





@wjandrea yup! but could I somehow avoid having to type out d1-d9? (its actually 42 dictionaries in the code so I'd really rather not do things manually). Looking for a way to iterate through my initial dictionary to generate d1-d9

– Plasmid
Mar 9 at 1:11













@Plasmid Yes, use a list comprehension.

– wjandrea
Mar 9 at 1:11





@Plasmid Yes, use a list comprehension.

– wjandrea
Mar 9 at 1:11












1 Answer
1






active

oldest

votes


















2














You can use a list comprehension to iterate a number from 0 to 8 and generate dicts of lists using a dict comprehension that outputs list items with possibly k added based on the quotient and remainder of 3 of the number:



[k: [a + ('k' if n // 3 + 1 == k and n % 3 == i else '') for i, a in enumerate(l)] for k, l in d.items() for n in range(9)]


so that given:



d = 1:['a','b','c'], 2:['e','f','g'], 3:['h','i','j']


this returns:



[1: ['ak', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'bk', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'ck'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['ek', 'f', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'fk', 'g'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'gk'], 3: ['h', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['hk', 'i', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'ik', 'j'],
1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'jk']]





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%2f55072984%2fiterating-through-dictionaries%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









    2














    You can use a list comprehension to iterate a number from 0 to 8 and generate dicts of lists using a dict comprehension that outputs list items with possibly k added based on the quotient and remainder of 3 of the number:



    [k: [a + ('k' if n // 3 + 1 == k and n % 3 == i else '') for i, a in enumerate(l)] for k, l in d.items() for n in range(9)]


    so that given:



    d = 1:['a','b','c'], 2:['e','f','g'], 3:['h','i','j']


    this returns:



    [1: ['ak', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
    1: ['a', 'bk', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
    1: ['a', 'b', 'ck'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
    1: ['a', 'b', 'c'], 2: ['ek', 'f', 'g'], 3: ['h', 'i', 'j'],
    1: ['a', 'b', 'c'], 2: ['e', 'fk', 'g'], 3: ['h', 'i', 'j'],
    1: ['a', 'b', 'c'], 2: ['e', 'f', 'gk'], 3: ['h', 'i', 'j'],
    1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['hk', 'i', 'j'],
    1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'ik', 'j'],
    1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'jk']]





    share|improve this answer



























      2














      You can use a list comprehension to iterate a number from 0 to 8 and generate dicts of lists using a dict comprehension that outputs list items with possibly k added based on the quotient and remainder of 3 of the number:



      [k: [a + ('k' if n // 3 + 1 == k and n % 3 == i else '') for i, a in enumerate(l)] for k, l in d.items() for n in range(9)]


      so that given:



      d = 1:['a','b','c'], 2:['e','f','g'], 3:['h','i','j']


      this returns:



      [1: ['ak', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
      1: ['a', 'bk', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
      1: ['a', 'b', 'ck'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
      1: ['a', 'b', 'c'], 2: ['ek', 'f', 'g'], 3: ['h', 'i', 'j'],
      1: ['a', 'b', 'c'], 2: ['e', 'fk', 'g'], 3: ['h', 'i', 'j'],
      1: ['a', 'b', 'c'], 2: ['e', 'f', 'gk'], 3: ['h', 'i', 'j'],
      1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['hk', 'i', 'j'],
      1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'ik', 'j'],
      1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'jk']]





      share|improve this answer

























        2












        2








        2







        You can use a list comprehension to iterate a number from 0 to 8 and generate dicts of lists using a dict comprehension that outputs list items with possibly k added based on the quotient and remainder of 3 of the number:



        [k: [a + ('k' if n // 3 + 1 == k and n % 3 == i else '') for i, a in enumerate(l)] for k, l in d.items() for n in range(9)]


        so that given:



        d = 1:['a','b','c'], 2:['e','f','g'], 3:['h','i','j']


        this returns:



        [1: ['ak', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
        1: ['a', 'bk', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
        1: ['a', 'b', 'ck'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
        1: ['a', 'b', 'c'], 2: ['ek', 'f', 'g'], 3: ['h', 'i', 'j'],
        1: ['a', 'b', 'c'], 2: ['e', 'fk', 'g'], 3: ['h', 'i', 'j'],
        1: ['a', 'b', 'c'], 2: ['e', 'f', 'gk'], 3: ['h', 'i', 'j'],
        1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['hk', 'i', 'j'],
        1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'ik', 'j'],
        1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'jk']]





        share|improve this answer













        You can use a list comprehension to iterate a number from 0 to 8 and generate dicts of lists using a dict comprehension that outputs list items with possibly k added based on the quotient and remainder of 3 of the number:



        [k: [a + ('k' if n // 3 + 1 == k and n % 3 == i else '') for i, a in enumerate(l)] for k, l in d.items() for n in range(9)]


        so that given:



        d = 1:['a','b','c'], 2:['e','f','g'], 3:['h','i','j']


        this returns:



        [1: ['ak', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
        1: ['a', 'bk', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
        1: ['a', 'b', 'ck'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'j'],
        1: ['a', 'b', 'c'], 2: ['ek', 'f', 'g'], 3: ['h', 'i', 'j'],
        1: ['a', 'b', 'c'], 2: ['e', 'fk', 'g'], 3: ['h', 'i', 'j'],
        1: ['a', 'b', 'c'], 2: ['e', 'f', 'gk'], 3: ['h', 'i', 'j'],
        1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['hk', 'i', 'j'],
        1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'ik', 'j'],
        1: ['a', 'b', 'c'], 2: ['e', 'f', 'g'], 3: ['h', 'i', 'jk']]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 1:14









        blhsingblhsing

        42.5k41743




        42.5k41743





























            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%2f55072984%2fiterating-through-dictionaries%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

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme