Garrett
Garrett

Reputation: 43

How do I update a value in a list, within a dictionary?

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

Answers (5)

Mayuri Bhalani
Mayuri Bhalani

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

Alexander
Alexander

Reputation: 109528

  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

Upvotes: 3

DavidGarciaFer
DavidGarciaFer

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

Cenk Bircanoglu
Cenk Bircanoglu

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

Reza Soltani
Reza Soltani

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

Related Questions