omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Setting values to each items in a list of a nested dictionary

    Pythonista
    python
    3
    5
    357
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • multitech_news
      multitech_news last edited by

      I'm trying to programmatically set a values to each item in a list inside a nested dictionary.

      for example, let's say my initial dictionary, which I constructed is:

      {'Abraham': {'subjects': ['Chem', 'Bio', 'Phy', 'Math']}}
      

      Given some lists of values:

      assess_type = ['Quiz', 'HW', 'ATTND', 'Exam']

      chem = ['127', '135', '17', '46']
      bio = ['154', '64', '14', '54']
      phy = ['115', '115', '15', '55']
      math = ['140', '160', '20', '40']
      And I want to achieve a result like this:

      {'Abraham': {'subjects': [
       'Chem': {'Quiz': '127', 'HW': '135', 'ATTND': '17', 'Exam': '46'},
       'Bio': {'Quiz': '154', 'HW': '64', 'ATTND': '14', 'Exam': '54'} ,
       'Phy': {'Quiz': '115', 'HW': '115', 'ATTND': '15', 'Exam': '55'},
       'Math': {'Quiz': '140', 'HW': '160', 'ATTND': '20', 'Exam': '40'}
        ]}}
      

      I can construct the dictionary itself, but where I am having issues is how to set the values programmatically. how do I index into the list inside the dictionary and set the value like that programmatically if I just have a list of values as written above?

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @multitech_news last edited by

        @multitech_news I guess that with

        {'Abraham': {'subjects': ['Chem': {'Quiz':....
        

        You mean

        {'Abraham': {'subjects': {'Chem': {'Quiz':....
        
        1 Reply Last reply Reply Quote 0
        • ccc
          ccc last edited by

          GitHub Co-pilot wrote:

          d = {
              "Abraham": {
                  "subjects": {
                      "Chem": {"Quiz": "127", "HW": "135", "ATTND": "17", "Exam": "46"},
                      "Bio": {"Quiz": "154", "HW": "64", "ATTND": "14", "Exam": "54"},
                      "Phy": {"Quiz": "115", "HW": "115", "ATTND": "15", "Exam": "55"},
                      "Math": {"Quiz": "140", "HW": "160", "ATTND": "20", "Exam": "40"},
                  }
              }
          }
          
          for student in d:
              for subject in d[student]["subjects"]:
                  for item in d[student]["subjects"][subject]:
                      print(item, d[student]["subjects"][subject][item])
                      d[student]["subjects"][subject][item] = 8
          
          cvp 1 Reply Last reply Reply Quote 0
          • cvp
            cvp @ccc last edited by

            @ccc I guess he wants to avoid typing several times the same texts like the access_types and subjects

            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by

              for subject in chem, bio, phy, math:
                  print({key: value for key, value in zip(assess_type, subject)})
              
              {'Quiz': '127', 'HW': '135', 'ATTND': '17', 'Exam': '46'}
              {'Quiz': '154', 'HW': '64', 'ATTND': '14', 'Exam': '54'}
              {'Quiz': '115', 'HW': '115', 'ATTND': '15', 'Exam': '55'}
              {'Quiz': '140', 'HW': '160', 'ATTND': '20', 'Exam': '40'}
              
              1 Reply Last reply Reply Quote 1
              • First post
                Last post
              Powered by NodeBB Forums | Contributors