Python Forum
How do you replace a dictionary key with a new input?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you replace a dictionary key with a new input?
#1
Good afternoon Python community,

I am practicing my Python skills by working on a log-in/registration script that asks if you are currently registered. If Y for 'Yes', then it runs the user through a log in validator. If 'No' then it runs the user through a sign-up form that validates each input field, and stores it into a dictionary userBase. At the end of the sign-up form, the program lists out all of the fields that were filled in to verify if the information is correct. If Y for "Yes" the program exits. If "N" for no, the program asks the user to choose which field they want to update, and re-prompts the field for them to update the selected information. This will loop until the user chooses Y for "Yes", and the program will exit.

My problem occurs when the user is asked to update a field. In a perfect world, the updated information will replace the appropriate dictionary key/value. However, I have not wrapped my finger around how exactly can I go about doing this. The original program is about 140 lines long, so I isolated the problem to 14 lines in order for you to see what I'm talking about:

import pprint

username = input('enter username: ')
password = input('Enter a password: ')
userBase = dict(
    user = {
        username:{'password': password}
        })


updateUsername = input('Enter in your new username: ')
userBase['user'][username] == updateUsername

pprint.pprint(userBase)
The output that I'm getting is:
Output:
enter username: John Enter in your new username: Jeff {'user': {'John': {'password':'12345abcde'}}}
The desired output is:
Output:
enter username: John Enter in your new username: Jeff {'user': {'Jeff': {'password':'12345abcde'}}}
I have searched around the internet for strategies on how to replace a dictionary key/value without entering an immutable string value, but nothing has worked for me so far. Is this something that I have to hack? Am I overlooking something that's very simple? Cry
Reply
#2
use the dictionary.update()

userBase.update(username : {"password" : password})
Reply
#3
Since you've already created a key in the dictionary, you can't just "rename" the key. You can copy it to a new key and then delete the old key. The canonical way to do this is to read the old name with a pop() and assign that to a new key.

# effectively renames key "old" to "new".  If "new" already existed,
# it will be destroyed.
d[new] = d.pop(old)  # effectively renames key "old" to "new".
Also, on your line 12 above you used == which tests equality rather than assigns new data.

So a modification might be:

import pprint

username = input('enter username: ')
password = input('Enter a password: ')
userBase = dict(
    user = {
        username:{'password': password}
        })


updateUsername = input('Enter in your new username: ')
userBase['user'][updateUsername] = userBase['user'].pop(username)

pprint.pprint(userBase)
Reply
#4
(Aug-22-2020, 07:34 PM)michael1789 Wrote: use the dictionary.update()

userBase.update(username : {"password" : password})

I tried this method but it returns a Syntax Error due to the ':' placed in the update parameter:

Error:
Traceback (most recent call last): File "/Users/gerrin/Desktop/Kali/Python/databaseProject/deleteLater.py", line 12 userBase.update(username : {"password" : password}) ^ SyntaxError: invalid syntax

(Aug-22-2020, 08:16 PM)bowlofred Wrote: Since you've already created a key in the dictionary, you can't just "rename" the key. You can copy it to a new key and then delete the old key. The canonical way to do this is to read the old name with a pop() and assign that to a new key.

# effectively renames key "old" to "new".  If "new" already existed,
# it will be destroyed.
d[new] = d.pop(old)  # effectively renames key "old" to "new".
Also, on your line 12 above you used == which tests equality rather than assigns new data.

So a modification might be:

import pprint

username = input('enter username: ')
password = input('Enter a password: ')
userBase = dict(
    user = {
        username:{'password': password}
        })


updateUsername = input('Enter in your new username: ')
userBase['user'][updateUsername] = userBase['user'].pop(username)

pprint.pprint(userBase)

Very helpful solution. This did the job! Thanks a lot Idea
Reply
#5
Real life scenario: what should happen if user updates name to what is already existing?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  User input & Dictionary tfernandes 5 3,593 Apr-03-2020, 07:12 PM
Last Post: tfernandes
  Get a value from a dictionary through input Anony 3 2,240 Jan-26-2020, 06:18 PM
Last Post: buran
  Problem using input in Dictionary.. Help roseojha 1 1,748 Aug-25-2019, 08:51 AM
Last Post: ThomasL
  Search & Replace - Newlines Added After Replace dj99 3 3,356 Jul-22-2018, 01:42 PM
Last Post: buran
  QC input value against dictionary in while loop dadgums 2 2,894 Apr-23-2018, 11:38 PM
Last Post: dadgums
  User Input to Choose from Dictionary anelliaf 9 25,674 Mar-27-2018, 02:22 PM
Last Post: anelliaf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020