What does python 3s dictionary update () method do?


Description

The method update[] adds dictionary dict2's key-values pairs in to dict. This function does not return anything.

Syntax

Following is the syntax for update[] method −

dict.update[dict2]

Parameters

dict2 − This is the dictionary to be added into dict.

Return Value

This method does not return any value.

Example

The following example shows the usage of update[] method.

#!/usr/bin/python3

dict = {'Name': 'Zara', 'Age': 7}
dict2 = {'Sex': 'female' }

dict.update[dict2]
print ["updated dict : ", dict]

Result

When we run above program, it produces the following result −

updated dict : {'Sex': 'female', 'Age': 7, 'Name': 'Zara'}

python_dictionary.htm

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python Dictionary update[] method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.

    Syntax: dict.update[[other]]

    Parameters: This method takes either a dictionary or an iterable object of key/value pairs [generally tuples] as parameters.

    Returns: It doesn’t return any value but updates the Dictionary with elements from a dictionary object or an iterable object of key/value pairs.

    Python Dictionary update[] Example

    Example #1: Update with another Dictionary

    Python3

    Dictionary1 = {'A': 'Geeks', 'B': 'For', }

    Dictionary2 = {'B': 'Geeks'}

    print["Original Dictionary:"]

    print[Dictionary1]

    Dictionary1.update[Dictionary2]

    print["Dictionary after updation:"]

    print[Dictionary1]

    Output: 

    Original Dictionary:
    {'A': 'Geeks', 'B': 'For'}
    
    Dictionary after updation:
    {'A': 'Geeks', 'B': 'Geeks'}

    Example #2: Update with an iterable

    Python3

    Dictionary1 = {'A': 'Geeks'}

    print["Original Dictionary:"]

    print[Dictionary1]

    Dictionary1.update[B='For', C='Geeks']

    print["Dictionary after updation:"]

    print[Dictionary1]

    Output: 

    Original Dictionary:
    {'A': 'Geeks'}
    
    Dictionary after updation:
    {'C': 'Geeks', 'B': 'For', 'A': 'Geeks'}

    Example #3: Python dictionary update value if the key exists

    Python3

    def checkKey[dict, key]:

        if key in dict.keys[]:

            print["Key exist, ", end =" "]

            dict.update[{'m':600}]

            print["value updated =", 600]

        else:

            print["Not Exist"]

    dict = {'m': 700, 'n':100, 't':500}

    key = 'm'

    checkKey[dict, key]

    print[dict]

    Output:

    Key exist,  value updated = 600
    {'m': 600, 'n': 100, 't': 500}

    What does Python 3's dictionary update [] method do?

    The method update[] adds dictionary dict2's key-values pairs in to dict. This function does not return anything.

    How does update [] in Python work?

    Python Set update[] Method The update[] method updates the current set, by adding items from another set [or any other iterable]. If an item is present in both sets, only one appearance of this item will be present in the updated set.

    What does dict update mean?

    The dict. update[] method updates the dictionary with the key-value pairs from another dictionary or another iterable such as tuple having key-value pairs.

    Which method in dictionary is used to update the dictionary items?

    update[] method updates the dictionary with elements from a dictionary object or an iterable object of key/value pairs.

    Chủ Đề