Reputation: 43
I have a list inside a dictionary like so:
d = {first_name + last_name : [first_name, last_name, email, phone_number, birthday]}
What I am trying to do is update the first_name value inside the list and the key. For example, my dictionary may look something like this:
{'johndoe' : ['john', 'doe', '[email protected]', '911', '01/09']}
I want to be able to update the name to jane or whatever:
{'janedoe' : ['jane', 'doe', '[email protected]', '911', '01/09']}
What I am trying:
def update_first_name(d, first_name, last_name, new_field_value):
d[first_name + last_name][0] == new_field_value
return d
I have also tried using the update() method but I don't know how to use it to change a specific index of a value. I also believe I am running into issues because I am trying to change a variable that is in both the key and the value of the dictionary.
Any help would be greatly appreciated!
Upvotes: 1
Views: 84
Reputation: 1
You can try this. I have even explained the flow of the code!
d = {'first_name' + 'last_name' : ['first_name', 'last_name', 'email', 'phone_number', 'birthday']}
def update_first_name(d, new_f_name):
# first we are finding the value list
values = (list(d.values()))[0]
# then we are extracting first andd last name
f_name = values[0]
l_name = values[1]
# we are changing the list with updated new first name
values[0] = new_f_name
# we create new key with the updated first name
key = new_f_name+l_name
# thereafter we delete the old key-value pair in d
del d[f_name+l_name]
# finally we insert new key-value pair in the 'd' and return that
d[key] = values
return d
m = update_first_name(d, 'radha')
print(m)
Upvotes: 0
Reputation: 109528
You may want to first ensure that the new key will not overwrite an existing record.
def update_first_name(d, first_name, last_name, new_field_value):
record = d[first_name + last_name]
del d[first_name + last_name]
record[0] = new_field_value
d[new_field_value + last_name] = record
return d
The above is written as is for clarity. As mentioned by @DavidGarcierFer and @CenkBircanoglu, you can use pop
to return the record and delete it in a single step. Also, you can supply a default value (e.g. None
) in case the record does not exist.
def update_first_name(d, first_name, last_name, new_field_value):
record = d.pop(first_name + last_name, None)
if not record:
return d # Or handle case of missing key, e.g. raise ValueError.
record[0] = new_field_value
d[new_field_value + last_name] = record
return d
Upvotes: 3
Reputation: 11
Using ==
operator returns a boolean value. You should use =
to assign a value.
d[first_name + last_name][0] = new_field_value
Then, if you want to rename the key of the dictionary you can do it using .pop()
(See Rename a dictionary key):
def update_first_name(d, first_name, last_name, new_field_value):
d[first_name + last_name][0] == new_field_value
d[new_field_value + last_name] = d.pop(first_name + last_name)
return d
Upvotes: 1
Reputation: 529
Can you try this?
>>> d = {'johndoe' : ['john', 'doe', '[email protected]', '911', '01/09']}
>>>
>>> def update_first_name(d, first_name, last_name, new_field_value):
... d[new_field_value+last_name] = d.pop(first_name + last_name)
... d[new_field_value+last_name][0] = new_field_value
... return d
...
>>> update_first_name(d, 'john', 'doe', 'jane')
{'janedoe': ['jane', 'doe', '[email protected]', '911', '01/09']}
Upvotes: 1
Reputation: 151
Keys cannot be modified. You have to delete the previous item from the dictionary and add the modified item to it.
Upvotes: 2