logo

Python Dictionary update() metode

Python Dictionary update() metode opdaterer ordbogen med elementerne fra et andet ordbogsobjekt eller fra en iterabel nøgle/værdi-par.

Eksempel:

  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'}>

Syntaks for Python Dictionary Update Method

Ordbogsopdatering()-metoden i Python har følgende syntaks:



Syntaks: dict.update([other])

Parametre: Denne metode tager enten en ordbog eller et iterabelt objekt af nøgle/værdi-par (generelt tupler) som parametre.

Vender tilbage: Det returnerer ikke nogen værdi, men opdaterer ordbogen med elementer fra et ordbogsobjekt eller et iterabelt objekt af nøgle/værdi-par.

konverter int til streng

Python Dictionary update() Eksempel

Lad os se et par eksempler på update()-metoden til at opdatere dataene i Python ordbog .

Opdater med en anden ordbog

Her opdaterer vi en ordbog i Python ved hjælp af update()-metoden og sender en anden ordbog til den som parametre. Den anden ordbog bruges til den opdaterede værdi.

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)>

>

css kommentar

>

Produktion:

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

Opdater med en Iterable

I dette eksempel, i stedet for at bruge en anden ordbog, sendte vi en iterable værdi til update()-funktionen.

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

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

Python Dictionary Update Value, hvis nøglen findes

I dette eksempel vil vi opdatere værdien af ​​en ordbog i Python, hvis den bestemte nøgle findes. Hvis nøglen ikke findes i ordbogen, udskriver vi blot, at nøglen ikke findes.

Python3


nussede nuller



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 Opdater værdi, hvis nøglen ikke eksisterer

Her vil vi forsøge at opdatere værdien af ​​den ordbog, hvis nøgle ikke findes i ordbogen. I dette tilfælde vil nøglen og værdien blive tilføjet som det nye element i ordbogen.

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>)>

ordbog initialisering c#
>

>

Produktion:

Key doesn't exist So, a new Key-Value pair will be created {'m': 700, 'n': 100, 't': 500, 'k': 600}>