Iterating through dictionaries Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!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?

Marquee sign letters

What is the proper term for etching or digging of wall to hide conduit of cables

How to make an animal which can only breed for a certain number of generations?

Dinosaur Word Search, Letter Solve, and Unscramble

Why do the Z-fighters hide their power?

The Nth Gryphon Number

My mentor says to set image to Fine instead of RAW — how is this different from JPG?

Flight departed from the gate 5 min before scheduled departure time. Refund options

Why are current probes so expensive?

French equivalents of おしゃれは足元から (Every good outfit starts with the shoes)

Inverse square law not accurate for non-point masses?

How can I list files in reverse time order by a command and pass them as arguments to another command?

What helicopter has the most rotor blades?

"Destructive power" carried by a B-52?

Is there night in Alpha Complex?

Weaponising the Grasp-at-a-Distance spell

Is a copyright notice with a non-existent name be invalid?

How can I prevent/balance waiting and turtling as a response to cooldown mechanics

Why can't fire hurt Daenerys but it did to Jon Snow in season 1?

Plotting a Maclaurin series

How to ask rejected full-time candidates to apply to teach individual courses?

Are there any irrational/transcendental numbers for which the distribution of decimal digits is not uniform?

Table formatting with tabularx?

Is honorific speech ever used in the first person?



Iterating through dictionaries



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!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

        44.9k51746




        44.9k51746





























            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

            Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

            Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

            Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved