Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Dictionary basics
Dictionaries in loops and conditional statements
Dictionaries in functions
15. Returning dictionaries from functions
Summary

Instruction

Good. What happens if you don't want to modify the original dictionary? Just like with lists, you can use the copy() function:

def zero_negative(dictionary_to_check):
  new_dictionary = dictionary_to_check.copy()
  for key in new_dictionary.keys():
    if new_dictionary[key] < 0:
      new_dictionary[key] = 0
  return new_dictionary

Mind that you could use dict() in order to create a copy of the dictionary, similar to what we did in the Working with lists section with the list() function to create a copy of a list. However, there is no difference between copy() and dict() for the chunk of code presented above.

Exercise

Modify the solution from the previous exercise so that it returns a new dictionary instead of modifying the dictionary passed in as an argument.

Stuck? Here's a hint!

Inside the function, create a copy of the original dictionary.