1

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', 'jd@gmail.com', '911', '01/09']}

I want to be able to update the name to jane or whatever:

{'janedoe' : ['jane', 'doe', 'jd@gmail.com', '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!

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Garrett
  • 43
  • 5
  • 1
    `==` is the _comparison_ operator. To assign a value, use `=`. Also, you don't really rename keys in a dictionary. You can simply delete the old key and assign the new value to he new key. – Pranav Hosangadi Nov 11 '20 at 17:42
  • Also, how are you calling the `update_first_name()` function? Please provide a [mre] that people can run to reproduce your problem. – Pranav Hosangadi Nov 11 '20 at 17:44

5 Answers5

3
  1. Get the original record
  2. Delete it from the dictionary
  3. Update the first name (the first field in the record)
  4. Insert it in the dictionary based on the new key

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
Alexander
  • 105,104
  • 32
  • 201
  • 196
2

Keys cannot be modified. You have to delete the previous item from the dictionary and add the modified item to it.

Reza Soltani
  • 151
  • 4
1

Can you try this?

>>> d = {'johndoe' : ['john', 'doe', 'jd@gmail.com', '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', 'jd@gmail.com', '911', '01/09']}
Cenk Bircanoglu
  • 499
  • 4
  • 10
1

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
0

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)