Home > Net >  Python, add strings to dictionary with loops
Python, add strings to dictionary with loops

Time:06-03

What I mean, is that the dictionary would fill up in this way:
{f"{username}": {"Username": username, "Password": password}}
(which should look like that:)
{"test1": {"Username": "test1", "Password": "test-password"}}
(...in this way,) so you can access the sub-dict over the username (f"{username}), and then in the sub-dict, it shows you the username and the password.
If I have for example a few usernames, I want to create new values in the main-dict, where the sub-dict gets added. (For example, you put the username and password into the sub-dict and after you have added that to the main-dict, you clear with with {dict}.clear()

All that should be possible to make in either a loop (doesn't matter if its while or for i in range()

I have tried to achieve that, with following code:

maindict = {}
subdict = {}

while True:
    username = input("Enter username: ")
    password = input("Enter password: ")

    subdict["username"] = username
    subdict["password"] = password
    maindict[username] = subdict
    print(maindict)

but my problem with that one is, if you run it once, it returns the right thing. But after the second time, the first value of the sub-dict gets overwritten with the second input and it creates a new the new value in the main-dict (which is correct, but it shouldn't overwrite the values of the sub-dict in the first value of the main-dict)
If you can't follow me there anymore, it prints out this:

Enter username: test1
Enter password: test1
{'test1': {'username': 'test1', 'password': 'test1'}}

Enter username: test2
Enter password: test2
{'test1': {'username': 'test2', 'password': 'test2'}, 'test2': {'username': 'test2', 'password': 'test2'}}

So as you see, it overwrites the "test1" values (of the sub-dict) and creates a second value in the main-dict.

Sorry if its confusing, I just couldn't find a solution for that. If I have explained it badly, just post a comment and ill give my best to explain it further.
Thanks! :)

CodePudding user response:

The problem seems to be that in the line maindict[username] = subdict, the value for all the keys is referring to same variable subdict, hence the repetition when you run it the second time.

To fix this, you can just replace the line with maindict[username] = subdict.copy(), which will ensure that the previous entries don't get overridden. This may not be the most performant solution, but it gets the job done.

CodePudding user response:

Initialize the empty subdict inside the loop, otherwise it always points to the same object in memory and the value are overwritten:

maindict = {}

while True:

    subdict = {}
    username = input("Enter username: ")
...

CodePudding user response:

Your are having this issue because you have subdict = {} outside of the while loop. So, each time you update value of username and password you are updating the same dictionary subdict, instead of creating a new dictionary for each user. So, you should keep the line subdict = {} inside your while loop, which will create a new dictionary for each user.

  • Related