Ruslan
Ruslan

Reputation: 43

How to to change the keys in a dictionary?

I have a dictionary of dog owners and i need to write a function update_age() that will update the dogs owner's ages in the dictionary. Here is an example:

dog_owners = {("John", "Malkovic", 22): ["Fido"],
              ("Jake", "Smirnoff", 18): ["Butch"],
              ("Simon", "Ng", 32): ["Zooma", "Rocky"],
              ("Martha", "Black", 73): ["Chase"]}

So the key is a tuple in which the numbers 22, 18, 32, and 73 represent age. The function update_age() has 3 arguments: name of dictionary, tuple with 3 elements, the number that should change the number from the tuple.

Example:

update_age(dog_owners, ("Jake", "Smirnoff", 18), 22)
dog_owners[("Jake", "Smirnoff", 22)] == ['Butch']
dog_owners.get(("Jake", "Smirnoff", 18)) is None

I tried to do:

def update_age(owners, owner, new_age):
    b = (new_age,)
    return owner[0:2] + b

but it is not correct because the original key still has the previous value and if i write:

print(update_age(dog_owners, ("Jake", "Smirnoff", 18), 22))   # for check
print(dog_owners.get(("Jake", "Smirnoff", 18)) is None)

the answer will be

('Jake', 'Smirnoff', 22)
False

So it means that my original key still has its previous value. How can I change it? May be I should try pop() and than add a new tuple to the key, but I don't know how.

Upvotes: 0

Views: 371

Answers (2)

Shakeel
Shakeel

Reputation: 2025

You can replace the keys to achieve this and moreover tuples are immutable.

You can also refactor code and restructure dictionary to make this use case achieve in simpler way but just in case if you want the way it is then you can check out below working snippet.

def update_age(owners, owner, new_age):
    if owner in owners:   # check if owner tuple exist in dictionary keys
        owner_list = list(owner)   # convert to list since tuples are immutable
        owner_list.pop(-1)   # remove last value
        owner_list.append(new_age)  # add new age at last

        owners.update({tuple(owner_list): owners.get(owner)})  # add new key updated tuple
        owners.pop(owner)  # remove old key
    return owners


dog_owners = {("John", "Malkovic", 22): ["Fido"],
              ("Jake", "Smirnoff", 18): ["Butch"],
              ("Simon", "Ng", 32): ["Zooma", "Rocky"],
              ("Martha", "Black", 73): ["Chase"]
              }

print(update_age(dog_owners, ("Jake", "Smirnoff", 18), 22))

Output:

{('John', 'Malkovic', 22): ['Fido'], ('Simon', 'Ng', 32): ['Zooma', 'Rocky'], ('Martha', 'Black', 73): ['Chase'], ('Jake', 'Smirnoff', 22): ['Butch']}

Tuples are immutable checkout why

Upvotes: 1

Prune
Prune

Reputation: 77850

Dict keys are exactly that: keys. You cannot "update" a key; it's the defining value. You have to delete the original and replace it with the new one; the two objects are not at related.

However, the problem here seems to be one of design: you stored information you want to alter as part of the key, for reasons you have not explained. Instead, make that information part of the entry's value:

          ("Simon Ng"): 
              {age: 32,
               dogs: ["Zooma", "Rocky"]},

Perhaps an even better idea would be to trade in your dict for a data frame.

Upvotes: 1

Related Questions