Python Dictionary update() metode
Python Dictionary update() metode oppdaterer ordboken med elementene fra et annet ordbokobjekt eller fra en gjentakelig nøkkel/verdi-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
Ordbokoppdateringen()-metoden i Python har følgende syntaks:
Syntaks: dict.update([other])
Parametere: Denne metoden tar enten en ordbok eller et iterbart objekt av nøkkel/verdi-par (vanligvis tupler) som parametere.
Returnerer: Den returnerer ingen verdi, men oppdaterer ordboken med elementer fra et ordbokobjekt eller et gjentakbart objekt av nøkkel/verdi-par.
Python Dictionary update() Eksempel
La oss se noen eksempler på update()-metoden for å oppdatere dataene til Python-ordbok .
Oppdater med en annen ordbok
Her oppdaterer vi en ordbok i Python ved å bruke update()-metoden og sender en annen ordbok til den som parametere. Den andre ordboken brukes for den oppdaterte verdien.
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)> |
Produksjon:
Original Dictionary: {'A': 'Geeks', 'B': 'For'} Dictionary after updation: {'A': 'Geeks', 'B': 'Geeks'} Oppdater med en Iterable
I dette eksemplet, i stedet for å bruke en annen ordbok, sendte vi en itererbar verdi til update()-funksjonen.
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)> |
Produksjon
Original Dictionary: {'A': 'Geeks'} Dictionary after updation: {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'} Python Dictionary Update Value hvis nøkkelen eksisterer
I dette eksemplet vil vi oppdatere verdien av en ordbok i Python hvis den bestemte nøkkelen eksisterer. Hvis nøkkelen ikke finnes i ordboken, vil vi ganske enkelt skrive ut at nøkkelen ikke finnes.
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> )> |
Produksjon:
Key exist, value updated = 600 {'m': 600, 'n': 100, 't': 500} Python Dictionary Update Value hvis nøkkelen ikke eksisterer
Her vil vi prøve å oppdatere verdien til ordboken hvis nøkkel ikke finnes i ordboken. I dette tilfellet vil nøkkelen og verdien legges til som det nye 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> )> |
Produksjon:
Key doesn't exist So, a new Key-Value pair will be created {'m': 700, 'n': 100, 't': 500, 'k': 600}