logo

Afegiu un parell clau:valor al diccionari de Python

Diccionari a Python és una col·lecció no ordenada de valors de dades, que s'utilitza per emmagatzemar valors de dades com un mapa, que a diferència d'altres tipus de dades que només contenen un valor únic com a element, el Diccionari conté el parell clau:valor. Quan utilitzem el diccionari, de vegades, hem d'afegir o modificar la clau/valor dins del diccionari. Vegem com afegir un parell clau:valor al diccionari de Python.

Codi #1: Ús de la notació de subíndex Aquest mètode crearà un nou parell clau:valor en un diccionari assignant un valor a aquesta clau.

Python 3






# Python program to add a key:value pair to dictionary> dict> => {>'key1'>:>'geeks'>,>'key2'>:>'for'>}> print>('Current>Dict> is>: ',>dict>)> > # using the subscript notation> # Dictionary_Name[New_Key_Name] = New_Key_Value> dict>[>'key3'>]>=> 'Geeks'> dict>[>'key4'>]>=> 'is'> dict>[>'key5'>]>=> 'portal'> dict>[>'key6'>]>=> 'Computer'> print>('Updated>Dict> is>: ',>dict>)>

>

instal·lació de maven
>

Sortida:

El dictat actual és: {'key2': 'for', 'key1': 'geeks'} El dictat actualitzat és: {'key3': 'Geeks', 'key5': 'portal', 'key6': 'Computer', 'key4': 'és', 'key1': 'geeks', 'key2': 'per'}

Complexitat temporal: O(1)
Espai auxiliar: O(1)

Codi #2: Utilitzant el mètode update().

Python 3




dict> => {>'key1'>:>'geeks'>,>'key2'>:>'for'>}> print>('Current>Dict> is>: ',>dict>)> # adding dict1 (key3, key4 and key5) to dict> dict1>=> {>'key3'>:>'geeks'>,>'key4'>:>'is'>,>'key5'>:>'fabulous'>}> dict>.update(dict1)> # by assigning> dict>.update(newkey1>=>'portal'>)> print>(>dict>)>

>

>

Sortida:

Dict actual és: {'key2': 'for', 'key1': 'geeks'} {'newkey1': 'portal', 'key4': 'és', 'key2': 'for', 'key1': 'geeks', 'key5': 'fabulós', 'key3': 'geeks'}

Complexitat temporal: O(1)
Espai auxiliar: O(1)

Codi #3: Prenent clau:valor com a entrada

Python 3




# Let's add key:value to a dictionary, the functional way> # Create your dictionary class> class> my_dictionary(>dict>):> ># __init__ function> >def> __init__(>self>):> >self> => dict>()> > ># Function to add key:value> >def> add(>self>, key, value):> >self>[key]>=> value> # Main Function> dict_obj>=> my_dictionary()> # Taking input key = 1, value = Geek> dict_obj.key>=> input>('Enter the key: ')> dict_obj.value>=> input>('Enter the value: ')> dict_obj.add(dict_obj.key, dict_obj.value)> dict_obj.add(>2>,>'forGeeks'>)> print>(dict_obj)>

>

>

Sortida:

 {'1': 'Geeks', 2: 'forGeeks'}>

Complexitat temporal: O(1)
Espai auxiliar: O(n)

Codi #4: Ús d'un diccionari de comprensió

Per exemple, podeu crear un diccionari nou que afegeixi un parell clau:valor a un diccionari existent com aquest:

Python 3


cpld vs fpga



existing_dict>=> {>'key1'>:>'value1'>,>'key2'>:>'value2'>}> new_key>=> 'key3'> new_value>=> 'value3'> updated_dict>=> {>*>*>existing_dict, new_key: new_value}> print>(updated_dict)> #This code is contributed by Edula Vinay Kumar Reddy>

>

>

Sortida

{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}>

Això crea un nou diccionari anomenat updated_dict que conté tots els parells clau:valor de l'existent_dict, així com el nou parell clau:valor 'clau3': 'valor3'.

Complexitat temporal: O(n)
Espai auxiliar: O(n)