Python dictionary becomes empty as soon as the function, which added items to it, finishes execution


I am trying to write a menu in which option 1 would add a key:value pair to a dictionary and option 2 would run a threading module using the items from the dictionary. Key is a message from the user input and Value is a number of seconds, from the user input, that a thread should pause for, before displaying the message.

Threading itself is irrelevant right now though. What I’m struggling with is the fact that when I use the function for option 1, it adds the key:value pairs to the dictionary successfully. But as soon as the function is finished the dictionary becomes empty again.

For a full description and the code please see this link.

Could somebody please help me figure this out?

Thank you in advance for your time and assistance. :slight_smile:



For anybody struggling with a similar problem in the future. It lied in the fact that the .zip() method can only be iterated over once. Hence any additional print(dict(zip(… methods would return an empty dictionary.

It was solved by equating the two user inputs from Option 1 to variables ‘message’ and ‘seconds’, and adding an additional line ‘messages_and_times[message] = seconds’ which combines the two inputs and adds them to the messages_and_times dictionary as a key:value pair.

1 Like