Generate list of values, based on matching keysRefactoring with python dictionary comprehensionCan python dictionary comprehension be used to create a dictionary of substrings and their locations?Dictionary Containing list data, filter based on value in listpython dictionary of dictionaries comprehension with nested loopsDelete a dictionary based on the value of a key in another list of dictionariesPython Set Comprehension Nested in Dict Comprehensiondictionary comprehension with list iteration based on value dimensionsCreating Python defaultdict using nested list of tuplesNeed help matching on a certain time a string is matched in a listPython: Print certain items from an API call that is a list and not a Dict

Succinct and gender-neutral Russian word for "writer"

Why do Thanos' punches not kill Captain America or at least cause vital wounds?

When do you stop "pushing" a book?

Why is PerfectForwardSecrecy considered OK, when it has same defects as salt-less password hashing?

Any suggestions on how to make this table prettier and suggestions on whether it's better to make it fit in 1 or 2 columns

Has magnetic core memory been used beyond the Moon?

Company threw a surprise party for the CEO, 3 weeks later management says we have to pay for it, do I have to?

What does formal training in a field mean?

Whats the purpose of lockedLoadData / uncached page build takes around a minute, spent in usleep

Are there variations of the regular runtimes of the Big-O-Notation?

Remove color cast in darktable?

Was Mohammed the most popular first name for boys born in Berlin in 2018?

Is it a good idea to copy a trader when investing?

Is it a Munchausen Number?

Why does increasing the sampling rate make implementing an anti-aliasing filter easier?

Getting a error after using setState with a promise

What's the difference between const array and static const array in C/C++

How are one-time password generators like Google Authenticator different from having two passwords?

Names of the Six Tastes

What is the name of meteoroids which hit Moon, Mars, or pretty much anything that isn’t the Earth?

Why did they go to Dragonstone?

Is it bad writing or bad story telling if first person narrative contains more information than the narrator knows?

What do "KAL." and "A.S." stand for in this inscription?

Electric kick drum pedal starts oscillating in such a way that it does not register hits



Generate list of values, based on matching keys


Refactoring with python dictionary comprehensionCan python dictionary comprehension be used to create a dictionary of substrings and their locations?Dictionary Containing list data, filter based on value in listpython dictionary of dictionaries comprehension with nested loopsDelete a dictionary based on the value of a key in another list of dictionariesPython Set Comprehension Nested in Dict Comprehensiondictionary comprehension with list iteration based on value dimensionsCreating Python defaultdict using nested list of tuplesNeed help matching on a certain time a string is matched in a listPython: Print certain items from an API call that is a list and not a Dict






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








0















I have the below dictionary:



'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1


How might I accomplish creating a list comprised of all values for a specified key? A list for all high values, or another list for all medium values?



The way I am currently accomplishing this doesn't seem like the best way. I've got a list of all severity levels, which I iterate over and compare such as shown below:



trace_list = ['High', 'Medium', 'Critical', 'Low']

total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1

for item in trace_labels:

y_values = []

for key, val in total_status_dict.items():
for ke in total_status_dict[key]:
if item is ke:
y_values.append(total_status_dict[key][ke])









share|improve this question






























    0















    I have the below dictionary:



    'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1


    How might I accomplish creating a list comprised of all values for a specified key? A list for all high values, or another list for all medium values?



    The way I am currently accomplishing this doesn't seem like the best way. I've got a list of all severity levels, which I iterate over and compare such as shown below:



    trace_list = ['High', 'Medium', 'Critical', 'Low']

    total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1

    for item in trace_labels:

    y_values = []

    for key, val in total_status_dict.items():
    for ke in total_status_dict[key]:
    if item is ke:
    y_values.append(total_status_dict[key][ke])









    share|improve this question


























      0












      0








      0








      I have the below dictionary:



      'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1


      How might I accomplish creating a list comprised of all values for a specified key? A list for all high values, or another list for all medium values?



      The way I am currently accomplishing this doesn't seem like the best way. I've got a list of all severity levels, which I iterate over and compare such as shown below:



      trace_list = ['High', 'Medium', 'Critical', 'Low']

      total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1

      for item in trace_labels:

      y_values = []

      for key, val in total_status_dict.items():
      for ke in total_status_dict[key]:
      if item is ke:
      y_values.append(total_status_dict[key][ke])









      share|improve this question
















      I have the below dictionary:



      'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1


      How might I accomplish creating a list comprised of all values for a specified key? A list for all high values, or another list for all medium values?



      The way I am currently accomplishing this doesn't seem like the best way. I've got a list of all severity levels, which I iterate over and compare such as shown below:



      trace_list = ['High', 'Medium', 'Critical', 'Low']

      total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1

      for item in trace_labels:

      y_values = []

      for key, val in total_status_dict.items():
      for ke in total_status_dict[key]:
      if item is ke:
      y_values.append(total_status_dict[key][ke])






      python-3.7 dictionary-comprehension






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 20 at 21:52







      NewToThis

















      asked Mar 20 at 20:51









      NewToThisNewToThis

      731110




      731110






















          1 Answer
          1






          active

          oldest

          votes


















          1














          Note: you are iterating over total_status_dict keys and appending results to a list. Remember that even if dictionaries are officially ordered in Python since 3.7 (see https://docs.python.org/3/whatsnew/3.7.html) you do not always control the Python version of the user. I would rather build a dict key -> item -> value, where key is Closed, Impact Statement Pending, ... and  item is one of the trace_labels than a dict key -> [values] where values is supposed to be ordered as in trace_labels.



          Your code is not efficient because you iterate over trace_labels twice:



          • for item in trace_labels:


          • for ke intotal_status_dict[key]: if item is ke:`

          How to iterate only once? Instead of building y_values lists one by one (with a whole iteration over total_status_dict each time), you can build several lists at once:



          >>> trace_labels = ['High', 'Medium', 'Critical', 'Low']
          >>> total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
          >>> y_values_by_label =
          >>> for key, value_by_label in total_status_dict.items():
          ... for label, value in value_by_label.items(): # total_status_dict[key] is value_by_label
          ... y_values_by_label.setdefault(label, )[key] = value
          ...
          >>> y_values_by_label
          'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1


          setdefault(label, ) creates a empty dict y_values_by_label[label] = if y_values_by_label does not have the key label.



          If you want to turn this in a dict comprehension, you have to use your inefficient method:



          >>> label:k:v for k, value_by_label in total_status_dict.items() for l, v in value_by_label.items() if l==label for label in trace_labels
          'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18





          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%2f55269978%2fgenerate-list-of-values-based-on-matching-keys%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














            Note: you are iterating over total_status_dict keys and appending results to a list. Remember that even if dictionaries are officially ordered in Python since 3.7 (see https://docs.python.org/3/whatsnew/3.7.html) you do not always control the Python version of the user. I would rather build a dict key -> item -> value, where key is Closed, Impact Statement Pending, ... and  item is one of the trace_labels than a dict key -> [values] where values is supposed to be ordered as in trace_labels.



            Your code is not efficient because you iterate over trace_labels twice:



            • for item in trace_labels:


            • for ke intotal_status_dict[key]: if item is ke:`

            How to iterate only once? Instead of building y_values lists one by one (with a whole iteration over total_status_dict each time), you can build several lists at once:



            >>> trace_labels = ['High', 'Medium', 'Critical', 'Low']
            >>> total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
            >>> y_values_by_label =
            >>> for key, value_by_label in total_status_dict.items():
            ... for label, value in value_by_label.items(): # total_status_dict[key] is value_by_label
            ... y_values_by_label.setdefault(label, )[key] = value
            ...
            >>> y_values_by_label
            'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1


            setdefault(label, ) creates a empty dict y_values_by_label[label] = if y_values_by_label does not have the key label.



            If you want to turn this in a dict comprehension, you have to use your inefficient method:



            >>> label:k:v for k, value_by_label in total_status_dict.items() for l, v in value_by_label.items() if l==label for label in trace_labels
            'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18





            share|improve this answer



























              1














              Note: you are iterating over total_status_dict keys and appending results to a list. Remember that even if dictionaries are officially ordered in Python since 3.7 (see https://docs.python.org/3/whatsnew/3.7.html) you do not always control the Python version of the user. I would rather build a dict key -> item -> value, where key is Closed, Impact Statement Pending, ... and  item is one of the trace_labels than a dict key -> [values] where values is supposed to be ordered as in trace_labels.



              Your code is not efficient because you iterate over trace_labels twice:



              • for item in trace_labels:


              • for ke intotal_status_dict[key]: if item is ke:`

              How to iterate only once? Instead of building y_values lists one by one (with a whole iteration over total_status_dict each time), you can build several lists at once:



              >>> trace_labels = ['High', 'Medium', 'Critical', 'Low']
              >>> total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
              >>> y_values_by_label =
              >>> for key, value_by_label in total_status_dict.items():
              ... for label, value in value_by_label.items(): # total_status_dict[key] is value_by_label
              ... y_values_by_label.setdefault(label, )[key] = value
              ...
              >>> y_values_by_label
              'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1


              setdefault(label, ) creates a empty dict y_values_by_label[label] = if y_values_by_label does not have the key label.



              If you want to turn this in a dict comprehension, you have to use your inefficient method:



              >>> label:k:v for k, value_by_label in total_status_dict.items() for l, v in value_by_label.items() if l==label for label in trace_labels
              'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18





              share|improve this answer

























                1












                1








                1







                Note: you are iterating over total_status_dict keys and appending results to a list. Remember that even if dictionaries are officially ordered in Python since 3.7 (see https://docs.python.org/3/whatsnew/3.7.html) you do not always control the Python version of the user. I would rather build a dict key -> item -> value, where key is Closed, Impact Statement Pending, ... and  item is one of the trace_labels than a dict key -> [values] where values is supposed to be ordered as in trace_labels.



                Your code is not efficient because you iterate over trace_labels twice:



                • for item in trace_labels:


                • for ke intotal_status_dict[key]: if item is ke:`

                How to iterate only once? Instead of building y_values lists one by one (with a whole iteration over total_status_dict each time), you can build several lists at once:



                >>> trace_labels = ['High', 'Medium', 'Critical', 'Low']
                >>> total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
                >>> y_values_by_label =
                >>> for key, value_by_label in total_status_dict.items():
                ... for label, value in value_by_label.items(): # total_status_dict[key] is value_by_label
                ... y_values_by_label.setdefault(label, )[key] = value
                ...
                >>> y_values_by_label
                'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1


                setdefault(label, ) creates a empty dict y_values_by_label[label] = if y_values_by_label does not have the key label.



                If you want to turn this in a dict comprehension, you have to use your inefficient method:



                >>> label:k:v for k, value_by_label in total_status_dict.items() for l, v in value_by_label.items() if l==label for label in trace_labels
                'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18





                share|improve this answer













                Note: you are iterating over total_status_dict keys and appending results to a list. Remember that even if dictionaries are officially ordered in Python since 3.7 (see https://docs.python.org/3/whatsnew/3.7.html) you do not always control the Python version of the user. I would rather build a dict key -> item -> value, where key is Closed, Impact Statement Pending, ... and  item is one of the trace_labels than a dict key -> [values] where values is supposed to be ordered as in trace_labels.



                Your code is not efficient because you iterate over trace_labels twice:



                • for item in trace_labels:


                • for ke intotal_status_dict[key]: if item is ke:`

                How to iterate only once? Instead of building y_values lists one by one (with a whole iteration over total_status_dict each time), you can build several lists at once:



                >>> trace_labels = ['High', 'Medium', 'Critical', 'Low']
                >>> total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
                >>> y_values_by_label =
                >>> for key, value_by_label in total_status_dict.items():
                ... for label, value in value_by_label.items(): # total_status_dict[key] is value_by_label
                ... y_values_by_label.setdefault(label, )[key] = value
                ...
                >>> y_values_by_label
                'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1


                setdefault(label, ) creates a empty dict y_values_by_label[label] = if y_values_by_label does not have the key label.



                If you want to turn this in a dict comprehension, you have to use your inefficient method:



                >>> label:k:v for k, value_by_label in total_status_dict.items() for l, v in value_by_label.items() if l==label for label in trace_labels
                'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 23 at 9:38









                jferardjferard

                2,7001315




                2,7001315





























                    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%2f55269978%2fgenerate-list-of-values-based-on-matching-keys%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

                    해남서초등학교 학교 연혁 학교 동문 외부 링크 둘러보기 메뉴북위 34° 34′ 21″ 동경 126° 35′ 50″ / 북위 34.5726385° 동경 126.5972670°  / 34.5726385; 126.5972670해남서초등학교eh

                    How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

                    Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?