Python Dictionary update() metod uppdaterar ordboken med elementen från ett annat ordboksobjekt eller från en iterabel nyckel/värdepar.
Exempel:
Original dictionary : {'A': 'Geeks', 'B': 'For'} Updated dictionary : {'A': 'Geeks', 'B': 'Geeks'} Original dictionary : {'A': 'Geeks', 'B': 'For'} Updated dictionary : {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}> Syntax för Python Dictionary Update Method
Dictionary update()-metoden i Pytonorm har följande syntax:
Syntax: dict.update([other])
Parametrar: Denna metod tar antingen en ordbok eller ett iterbart objekt av nyckel/värdepar (vanligtvis tupler) som parametrar.
Returnerar: Den returnerar inget värde utan uppdaterar ordboken med element från ett ordboksobjekt eller ett iterbart objekt av nyckel/värdepar.
Python Dictionary update() Exempel
Låt oss se några exempel på update()-metoden för att uppdatera data för Python ordbok .
Uppdatera med en annan ordbok
Här uppdaterar vi en ordbok i Python med metoden update() och skickar en annan ordbok till den som parametrar. Den andra ordboken används för det uppdaterade värdet.
Python3
# Python program to show working> # of update() method in Dictionary> # Dictionary with three items> Dictionary1>=> {>'A'>:>'Geeks'>,>'B'>:>'For'>, }> Dictionary2>=> {>'B'>:>'Geeks'>}> # Dictionary before Updation> print>(>'Original Dictionary:'>)> print>(Dictionary1)> # update the value of key 'B'> Dictionary1.update(Dictionary2)> print>(>'Dictionary after updation:'>)> print>(Dictionary1)> |
>
>
Produktion:
Original Dictionary: {'A': 'Geeks', 'B': 'For'} Dictionary after updation: {'A': 'Geeks', 'B': 'Geeks'}> Uppdatera med en Iterable
I det här exemplet skickade vi ett iterbart värde till funktionen update() istället för att använda en annan ordbok.
Python3
# Python program to show working> # of update() method in Dictionary> # Dictionary with single item> Dictionary1>=> {>'A'>:>'Geeks'>}> # Dictionary before Updation> print>(>'Original Dictionary:'>)> print>(Dictionary1)> # update the Dictionary with iterable> Dictionary1.update(B>=>'For'>, C>=>'Geeks'>)> print>(>'Dictionary after updation:'>)> print>(Dictionary1)> |
>
>Produktion
gigabyte vs megabyte
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}> Python Dictionary Update Value om nyckeln finns
I det här exemplet kommer vi att uppdatera värdet på en ordbok i Python om den specifika nyckeln finns. Om nyckeln inte finns i ordboken skriver vi helt enkelt ut att nyckeln inte finns.
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>)> |
>
>
Produktion:
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500}> Python Dictionary Uppdatera värde om nyckeln inte finns
Här kommer vi att försöka uppdatera värdet på ordboken vars nyckel inte finns i ordboken. I det här fallet kommer nyckeln och värdet att läggas till som det nya elementet i ordboken.
Python3
def> checkKey(>dict>, key):> > >if> key>not> in> dict>.keys():> >print>(>'Key doesn't exist So, a new Key-Value pair will be created'>)> >dict>.update({key:>600>})> >else>:> >print>(>'Key Exist'>)> dict> => {>'m'>:>700>,>'n'>:>100>,>'t'>:>500>}> > key>=> 'k'> checkKey(>dict>, key)> print>(>dict>)> |
>
>
Produktion:
Key doesn't exist So, a new Key-Value pair will be created {'m': 700, 'n': 100, 't': 500, 'k': 600}>