Adăugați o pereche cheie:valoare în dicționar în Python

Dicţionar în Python este o colecție neordonată de valori de date, folosită pentru a stoca valorile datelor ca o hartă, care, spre deosebire de alte tipuri de date care dețin doar o singură valoare ca element, Dicționarul conține pereche cheie:valoare. Când folosim Dicționar, uneori, trebuie să adăugăm sau să modificăm cheia/valoarea din dicționar. Să vedem cum să adăugați o pereche cheie:valoare la dicționar în Python.

Codul #1: Folosind notația subscript Această metodă va crea o nouă pereche cheie:valoare pe un dicționar prin alocarea unei valori acelei chei.

Python3






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

Ieșire:

Dict-ul curent este: {'key2': 'for', 'key1': 'geeks'} Dict-ul actualizat este: {'key3': 'Geeks', 'key5': 'portal', 'key6': 'Computer', „key4”: „este”, „key1”: „geeks”, „key2”: „for”}

Complexitatea timpului: O(1)
Spațiu auxiliar: O(1)

Codul #2: Folosind metoda update().

Python3




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

Ieșire:

Dict curent este: {'key2': 'for', 'key1': 'geeks'} {'newkey1': 'portal', 'key4': 'est', 'key2': 'for', 'key1': „geeks”, „key5”: „fabulous”, „key3”: „geeks”}

Complexitatea timpului: O(1)
Spațiu auxiliar: O(1)

Codul #3: Luând Key:value ca intrare

Python3




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

Ieșire:

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

Complexitatea timpului: O(1)
Spațiu auxiliar: Pe)

Codul #4: Folosind o înțelegere a dicționarului

De exemplu, puteți crea un nou dicționar care adaugă o pereche cheie:valoare la un dicționar existent, astfel:

Python3




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>

Ieșire

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

Acest lucru creează un nou dicționar numit updated_dict care conține toate perechile cheie:valoare din existent_dict, precum și noua pereche cheie:valoare „key3”: „value3”.

Complexitatea timpului: Pe)
Spațiu auxiliar: Pe)



Top Articole

Categorie

Articole Interesante