1

The goal of the function is to make a grade adjustment based off of a dictionary and list. For instance

def adjust_grades(roster, grade_adjustment) 
adjust_grades({'ann': 75, 'bob': 80}, [5, -5])

will return

{'ann': 80, 'bob': 75}

I just need a nudge in the right direction, I'm new to Python so I thought to put a nested for loop for each num in grade_adjustment but its not the right way.

Jazib
1,37914 silver badges27 bronze badges
asked Sep 28, 2019 at 18:56
5
  • You should try defining your dictionary as an OrderedDict. Or define your grade_adjustment as a dictionary: {'ann' : 5, 'bob' : -5 } Commented Sep 28, 2019 at 19:01
  • 4
    @J.Murray, That was true till 3.6. In 3.6 order preserving feature was considered implementation detail. In 3.7+ order-preserving feature is guaranteed Commented Sep 28, 2019 at 19:01
  • The grade_adjustment should also be a dictionary. Then, for name, adj in grade_adjustment.items(): roster[name] += adj Commented Sep 28, 2019 at 19:02
  • @Buran Nice! Its a solid edition. I think that I'd still consider alternatives than rely on that (esp if you don't know what version of the interpreter the code may be executed via) Commented Sep 28, 2019 at 19:03
  • @Alexander That makes a lot of sense, unfortunately its a solution I cannot use for this assignment. It HAS to be a list. Commented Sep 28, 2019 at 19:05

4 Answers 4

1

Assuming Python 3.7 (ordered dicts) and the length of the adjustments match the length of the items in the dictionary, you can zip them together as follows:

for name, adjustment_amount in zip(roster, grade_adjustment):
 roster[name] += adjustment_amount
>>> roster
{'ann': 80, 'bob': 75}
answered Sep 28, 2019 at 19:12
Sign up to request clarification or add additional context in comments.

1 Comment

Very simple and easy to follow solution -- thank you!
1

This is making several assumptions:

  • the dictionary and the list have the same length (your final code should make sure they do)
  • you are using a version of python in which the order of the dictionary keys is preserved (if not, you can make grade_adjustment a dictionary as well, as mentioned by other comments)
result = roster.copy()
for index, key in enumerate(roster):
 result[key] += grade_adjustment[index]
answered Sep 28, 2019 at 19:06

1 Comment

Ah! Very helpful, that makes a lot of sense. I will try this, thank you.
1

You can use

def adjust_grades(roster, grade_adjustment):
 for k, v in enumerate(grade_adjustment):
 roster[list(roster.keys())[k]] = roster[list(roster.keys())[k]] + v
 return roster

This gives output as you said {'ann': 80, 'bob': 75}

answered Sep 28, 2019 at 19:08

1 Comment

I will try this too, thank you so much for your help!
1

assuming 3.7 or ordered dict and equal length:

def adjust_grades(roster, grade_adjustment):
 return {key:value + adjustment for (key, value), adjustment in
 zip(roster.items(), grade_adjustment)}
print(adjust_grades({'ann': 75, 'bob': 80}, [5, -5]))
answered Sep 28, 2019 at 19:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.