Python Dictionary implementation guide Correctly When Using a Function to Modify Values
I've been struggling with this for a few days now and could really use some help. I'm working on a personal project and I'm working with an scenario where my dictionary is not updating correctly when I try to modify its values using a function. I have a dictionary that holds user preferences, and I want to apply a transformation function to all the values. However, it seems that the original dictionary remains unchanged after the operation. Here's a snippet of my code: ```python user_preferences = {'theme': 'dark', 'notifications': 'enabled', 'font_size': 12} def update_preferences(pref_dict, update_func): for key in pref_dict: pref_dict[key] = update_func(pref_dict[key]) def increment_font_size(size): return size + 2 update_preferences(user_preferences, increment_font_size) print(user_preferences) ``` When I run this code, I expect the `font_size` to increment by 2, but it outputs: `{'theme': 'dark', 'notifications': 'enabled', 'font_size': 12}`. The dictionary appears unchanged. Iβve verified that the `increment_font_size` function is indeed being called and returning the correct value, but for some reason, it doesnβt update the dictionary as expected. I've also tried printing the values inside the loop to ensure they are correctly received by the function: ```python def update_preferences(pref_dict, update_func): for key in pref_dict: print(f'Updating {key}: {pref_dict[key]}') # Debug print pref_dict[key] = update_func(pref_dict[key]) ``` This prints the expected outputs, showing that the function is definitely being called with the right values. But still, the dictionary does not change. Is there something I'm missing about how dictionaries work in Python, or is it possible that I'm working with a scoping scenario? I'm using Python 3.9.6. If anyone has any insights into why my dictionary isn't updating, I'd greatly appreciate it! My development environment is macOS. Thanks in advance! How would you solve this?