Dizionario in Python è una raccolta non ordinata di valori di dati, utilizzata per archiviare valori di dati come una mappa, che a differenza di altri tipi di dati che contengono solo un singolo valore come elemento, il dizionario contiene una coppia chiave:valore. Durante l'utilizzo del dizionario, a volte, è necessario aggiungere o modificare la chiave/valore all'interno del dizionario. Vediamo come aggiungere una coppia chiave:valore al dizionario in Python.
Codice n. 1: Utilizzo della notazione in pedice Questo metodo creerà una nuova coppia chiave:valore su un dizionario assegnando un valore a quella chiave.
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> )> |
>
come creare un array in Java
>Produzione:
Il Dict attuale è: {'key2': 'for', 'key1': 'geeks'} Il Dict aggiornato è: {'key3': 'Geeks', 'key5': 'portal', 'key6': 'Computer', 'key4': 'is', 'key1': 'geeks', 'key2': 'for'}
Complessità temporale: O(1)
Spazio ausiliario: O(1)
Codice n. 2: Utilizzando il metodo 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> )> |
>
>Produzione:
Il Dict attuale è: {'key2': 'for', 'key1': 'geeks'} {'newkey1': 'portal', 'key4': 'is', 'key2': 'for', 'key1': 'geeks', 'key5': 'favoloso', 'key3': 'geeks'}
Complessità temporale: O(1)
Spazio ausiliario: O(1)
Codice n. 3: Prendendo Chiave:valore come input
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)> |
>
>
Produzione:
{'1': 'Geeks', 2: 'forGeeks'}>
Complessità temporale: O(1)
Spazio ausiliario: SU)
Codice n. 4: Utilizzando un dizionario di comprensione
Ad esempio, puoi creare un nuovo dizionario che aggiunge una coppia chiave:valore a un dizionario esistente in questo modo:
Python3
tipo restituito in Java
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> |
>
>Produzione
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}>
Questo crea un nuovo dizionario chiamato aggiornato_dict che contiene tutte le coppie chiave:valore da esistente_dict, così come la nuova coppia chiave:valore 'chiave3': 'valore3'.
Complessità temporale: SU)
Spazio ausiliario: SU)