logo

Fügen Sie dem Wörterbuch in Python ein Schlüssel:Wert-Paar hinzu

Wörterbuch In Python handelt es sich um eine ungeordnete Sammlung von Datenwerten, die zum Speichern von Datenwerten wie eine Karte verwendet wird. Im Gegensatz zu anderen Datentypen, die nur einen einzelnen Wert als Element enthalten, enthält das Wörterbuch ein Schlüssel:Wert-Paar. Bei der Verwendung von Dictionary müssen wir manchmal den Schlüssel/Wert im Wörterbuch hinzufügen oder ändern. Sehen wir uns an, wie man in Python ein Schlüssel-Wert-Paar zum Wörterbuch hinzufügt.

Code Nr. 1: Unter Verwendung der Subscript-Notation erstellt diese Methode ein neues Schlüssel-Wert-Paar in einem Wörterbuch, indem sie diesem Schlüssel einen Wert zuweist.

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

>

.06 als Bruch
>

Ausgabe:

Aktuelles Diktat ist: {'key2': 'for', 'key1': 'geeks'} Aktualisiertes Diktat ist: {'key3': 'Geeks', 'key5': 'portal', 'key6': 'Computer', 'key4': 'is', 'key1': 'geeks', 'key2': 'for'}

Zeitkomplexität: O(1)
Hilfsraum: O(1)

Code Nr. 2: Verwenden der update()-Methode

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

>

>

Ausgabe:

Aktuelles Dict ist: {'key2': 'for', 'key1': 'geeks'} {'newkey1': 'portal', 'key4': 'is', 'key2': 'for', 'key1': 'geeks', 'key5': 'fabulous', 'key3': 'geeks'}

Zeitkomplexität: O(1)
Hilfsraum: O(1)

Code Nr. 3: Key:value als Eingabe verwenden

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

>

>

Ausgabe:

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

Zeitkomplexität: O(1)
Hilfsraum: An)

Code Nr. 4: Verwenden eines Wörterbuchverständnisses

Sie können beispielsweise ein neues Wörterbuch erstellen, das einem vorhandenen Wörterbuch ein Schlüssel-Wert-Paar hinzufügt:

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>

>

>

Ausgabe

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

Dadurch wird ein neues Wörterbuch namens „update_dict“ erstellt, das alle Schlüssel:Wert-Paare aus „existierendes_dict“ sowie das neue Schlüssel:Wert-Paar „key3“: „value3“ enthält.

jframe

Zeitkomplexität: An)
Hilfsraum: An)