Sayantan's Blog On Python Programming

UPDATING PYTHON DICTIONARIES

UPDATING PYTHON DICTIONARIES

INTRODUCTIONS

Like inserting new items in the dictionary, we can modify the dictionaries as well.

Updating existing dictionary value

# UPDATING PYTHON DICTIONARIES
# Example 1


dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

dict_class_wise_count['E'] = 600

print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

UPDATING PYTHON DICTIONARIES : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 600}

In the above example, we have updated the value of key “E“.

# UPDATING PYTHON DICTIONARIES
# Example 2


dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

dict_class_wise_count['B'] = 1200
dict_class_wise_count['C'] = dict_class_wise_count['C']*6

print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

UPDATING PYTHON DICTIONARIES : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 1200, 'C': 1800, 'D': 400, 'E': 500}

In the above example, we have modified the values against keys B and C.

# UPDATING PYTHON DICTIONARIES
# Example 3


dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':500}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

dict_class_wise_count['E'] = [500, 501, 502]

print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

UPDATING PYTHON DICTIONARIES : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': 500}

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502]}

In the above example, we have updated the value against key “E” with a list item.

Adding new dictionary value against existing key

# UPDATING PYTHON DICTIONARIES
# Example 4

dict_class_wise_count = {'A':100, 'B':200, 'C':300, 'D':400, 'E':[500, 501, 502]}
print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

dict_class_wise_count['E'].append(503)

print("\nList of key value pair of dict_class_wise_count ->", dict_class_wise_count)

UPDATING PYTHON DICTIONARIES : Output

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502]}

List of key value pair of dict_class_wise_count -> {'A': 100, 'B': 200, 'C': 300, 'D': 400, 'E': [500, 501, 502, 503]}

In the above example, we have used a list item against the key “E“. Then we have added a new element 503 inside the list.

# UPDATING PYTHON DICTIONARIES
# Example 5

dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

dict_class_wise_prog['C'] = {'C1':'PHP', 'C2':'XML'}

print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

UPDATING PYTHON DICTIONARIES : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': {'C1': 'PHP', 'C2': 'XML'}}

In the above example, we have updated the value against the key “C” with a dictionary.

update() method

  • update() method is used to update an existing dictionary
  • It can update the value against an existing key or it can add new key value pair

Updating an existing dictionary value

# UPDATING PYTHON DICTIONARIES
# Example 6

dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

dict_class_wise_prog.update({'C':'PHP'})

print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

UPDATING PYTHON DICTIONARIES : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'PHP'}

In the above example, we have updated the value against key “C“.

Adding new key value pair

# UPDATING PYTHON DICTIONARIES
# Example 7

dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

dict_class_wise_prog.update({'D':'PHP', 'E':'XML'})

print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

UPDATING PYTHON DICTIONARIES : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}

In the above example, we have added two new key value pair.

# UPDATING PYTHON DICTIONARIES
# Example 8

dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)

dict_class_wise_prog.update({'D':'PHP', 'E':'XML'})
dict_class_wise_prog_copy = dict_class_wise_prog;

print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
print("\nList of key value pair of dict_class_wise_prog_copy ->", dict_class_wise_prog_copy)

UPDATING PYTHON DICTIONARIES : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}

List of key value pair of dict_class_wise_prog_copy -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}

In the above example, we have added two new key value pair and then we have created a new dictionary based on existing dictionary.

Using ** operator

** operator helps us to merge one dictionary data into other dictionary along with additional items.

# UPDATING PYTHON DICTIONARIES
# Example 9

dict_class_wise_prog = {'A':'Python', 'B':'Java', 'C':'C++'}
print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)


dict_class_wise_prog_copy = {**dict_class_wise_prog, **{'D':'PHP', 'E':'XML'}}

print("\nList of key value pair of dict_class_wise_prog ->", dict_class_wise_prog)
print("\nList of key value pair of dict_class_wise_prog_copy ->", dict_class_wise_prog_copy)

UPDATING PYTHON DICTIONARIES : Output

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog -> {'A': 'Python', 'B': 'Java', 'C': 'C++'}

List of key value pair of dict_class_wise_prog_copy -> {'A': 'Python', 'B': 'Java', 'C': 'C++', 'D': 'PHP', 'E': 'XML'}

In the above example, we have used the ** operator use create and new dictionary dict_class_wise_prog_copy from dict_class_wise_prog along with two new key value pair.

UPDATING PYTHON DICTIONARIES : Output
UPDATING PYTHON DICTIONARIES : Output

RELATED TOPICS:

Leave a Comment

Your email address will not be published. Required fields are marked *