Is it possible to add a key to a Python dictionary after it has been created? It doesn't seem to have an .add()
method.
Jun 02, 2019 @hegash the dkey=val syntax as it is shorter and can handle any object as key (as long it is hashable), and only sets one value, whereas the.update(key1=val1, key2=val2) is nicer if you want to set multiple values at the same time, as long as the keys are strings (since kwargs are converted to strings).dict.update can also take another dictionary, but I personally prefer not to explicitly.
Use a custom dictionary in Office for Mac. In addition, some words might be capitalized differently in the main dictionary than in your file. Adding such words or capitalization to a custom dictionary prevents the spelling checker from flagging them as mistakes. You can create additional custom dictionaries to complement the default. Sep 19, 2015 Fix/Solution: Add a Custom Dictionary and select a language in Proofing options. Click File – Options; Select Proofing – Custom Dictionaries; Select the CUSTOM.DIC and a Language (English (United States) in my case) if not selected. If you don’t see your language, add it. Click OK’s to close all dialog boxes. Your problem is fixed.
kmario23To add multiple keys simultaneously:
For adding a single key, the accepted answer has less computational overhead.
jeremysprofileI feel like consolidating info about Python dictionaries:
Feel free to add more!
Yugal JindleYugal JindleYes it is possible, and it does have a method that implements this, but you don't want to use it directly.
To demonstrate how and how not to use it, let's create an empty dict with the dict literal, {}
:
To update this dict with a single new key and value, you can use the subscript notation (see Mappings here) that provides for item assignment:
my_dict
is now:
update
method - 2 waysWe can also update the dict with multiple values efficiently as well using the update
method. We may be unnecessarily creating an extra dict
here, so we hope our dict
has already been created and came from or was used for another purpose:
my_dict
is now:
Another efficient way of doing this with the update method is with keyword arguments, but since they have to be legitimate python words, you can't have spaces or special symbols or start the name with a number, but many consider this a more readable way to create keys for a dict, and here we certainly avoid creating an extra unnecessary dict
:
and my_dict
is now:
So now we have covered three Pythonic ways of updating a dict
.
There's another way of updating a dict
that you shouldn't use, which uses the __setitem__
method. Here's an example of how one might use the __setitem__
method to add a key-value pair to a dict
, and a demonstration of the poor performance of using it:
So we see that using the subscript notation is actually much faster than using __setitem__
. Doing the Pythonic thing, that is, using the language in the way it was intended to be used, usually is both more readable and computationally efficient.
If you want to add a dictionary within a dictionary you can do it this way.
Example: Add a new entry to your dictionary & sub dictionary
Output:
NOTE: Python requires that you first add a sub
before adding entries.
The orthodox syntax is d[key] = value
, but if your keyboard is missing the square bracket keys you could do:
In fact, defining __getitem__
and __setitem__
methods is how you can make your own class support the square bracket syntax. See https://python.developpez.com/cours/DiveIntoPython/php/endiveintopython/object_oriented_framework/special_class_methods.php
This popular question addresses functional methods of merging dictionaries a
and b
.
Here are some of the more straightforward methods (tested in Python 3)...
Note: The first method above only works if the keys in b
are strings.
To add or modify a single element, the b
dictionary would contain only that one element...
This is equivalent to...
Let's pretend you want to live in the immutable world and do NOT want to modify the original but want to create a new dict
that is the result of adding a new key to the original.
In Python 3.5+ you can do:
The Python 2 equivalent is:
After either of these:
params
is still equal to {'a': 1, 'b': 2}
and
new_params
is equal to {'a': 1, 'b': 2, 'c': 3}
There will be times when you don't want to modify the original (you only want the result of adding to the original). I find this a refreshing alternative to the following:
or
Reference: https://stackoverflow.com/a/2255892/514866
campetersoncampetersonSo many answers and still everybody forgot about the strangely named, oddly behaved, and yet still handy dict.setdefault()
This
basically just does this:
e.g.
Michael EkokaMichael EkokaIf you're not joining two dictionaries, but adding new key-value pairs to a dictionary, then using the subscript notation seems like the best way.
However, if you'd like to add, for example, thousands of new key-value pairs, you should consider using the update()
method.
first to check whether the key already exists
then you can add the new key and value
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
A custom dictionary is useful if, for example, your work relies on a particular terminology, such as legal terms. Alternatively, you could create a custom dictionary that includes the names of your work contacts so that you know you're always spelling their names correctly.
When you use the spelling checker, it compares the words in your document with those in the main dictionary. The main dictionary contains the most common words, but it might not include proper names, technical terms, or acronyms. In addition, some words might be capitalized differently in the main dictionary than in your file. Adding such words or capitalization to a custom dictionary prevents the spelling checker from flagging them as mistakes. You can create additional custom dictionaries to complement the default dictionary that is included with the Office for Mac applications.
Note: Any custom dictionary setting that you change in one Office for Mac application affects all the other applications. For example, you set many of the custom dictionary settings in Word, and then see the changes the next time you open PowerPoint or Excel.
Add a word from your document to a custom dictionary
In the document, select the word that you want to add.
On the Tools menu, click Spelling and Grammar.
Note: The Spelling and Grammar dialog box won't open if no spelling or grammar errors are detected, or if the word you are trying to add already exists in the dictionary.
In the Add words to menu, select the dictionary you want to add your word to.
Click Add.
Create a new custom dictionary in Word
You can save a new custom dictionary in any location on your computer. Or, to give other users access to your dictionary, you can save it to a network folder
Note: Any custom dictionary that you create in Word is used by other Office for Mac applications.
Make sure that a document is open.
If no document is open, the next step won't work.
On the Word menu, click Preferences.
Under Authoring and Proofing Tools, click Spelling and Grammar.
Under Spelling, click Dictionaries.
Click New.
Important: If the Convert File or File Conversion dialog boxes open, don't make any changes—just click OK. (The correct text encoding setting is Other encoding: Unicode 5.1 (Little-Endian).)
In the Save As box, type a name for the custom dictionary.
Note: When you create a new custom dictionary, Office for Mac automatically begins using it to check spelling.
Click Save, and then click OK again to close the Spelling and Grammar dialog box.
Import a custom dictionary in Word
The Custom Dictionaries dialog box in Word lists the available custom dictionaries the application can use to check spelling. If the dictionary that you want to use — for example, one that you purchased from a third-party company — is installed on your computer but not listed in the Dictionary list box, you can add it.
Make sure that a document is open.
If no document is open, the next step won't work.
On the Word menu, click Preferences.
Under Authoring and Proofing Tools, click Spelling and Grammar.
Under Spelling, click Dictionaries.
Click Add, and then locate and double-click the custom dictionary that you want to import.
If the custom dictionary doesn't have a file name extension of .dic, on the Enable pop-up menu, click All Files.
Add, delete, or edit words in a custom dictionary in Word
You can edit a custom dictionary in Word. You can change words that are in the dictionary, or add more words. When you edit a custom dictionary, however, Word does not automatically begin using it; you must select it. The revised version of the dictionary is available in all Office applications.
Make sure that a document is open.
If no document is open, the next step won't work.
On the Word menu, click Preferences.
Under Authoring and Proofing Tools, click Spelling and Grammar.
Under Spelling, click Dictionaries.
Click the name of the dictionary that you want to edit, and then click Edit.
If you see a message that Word stops checking spelling when you edit the dictionary, click OK, and then if you see the File Conversion - Custom Dictionary dialog box, click OK.
Important: If the Convert File or File Conversion dialog boxes open, don't make any changes—just click OK. (The correct text encoding setting is Other encoding: Unicode 5.1 (Little-Endian).)
In the dictionary file, make any changes that you want, and then on the File menu, click Save.
Note: You must type each word that you add on a separate line.
On the File menu, click Close.
Change the language that is associated with a custom dictionary
By default, when you create a new custom dictionary, the application sets the dictionary to All Languages, which means that the dictionary is used when you check the spelling of text in any language. However, you can associate a custom dictionary with a particular language so that the application uses the dictionary only when you check the spelling of text in a particular language.
Make sure that a document is open.
If no document is open, the next step won't work.
On the Word menu, click Preferences.
Under Authoring and Proofing Tools, click Spelling and Grammar.
Under Spelling, click Dictionaries.
Click the name of the dictionary that you want to edit, and then on the Language pop-up menu, click the language you want to associate with the custom dictionary.
Notes:
If you installed Office 2016 for Mac to run side-by-side with Office for Mac 2011, any custom dictionaries that you added and enabled in Office for Mac 2011 will be automatically copied to Office 2016 for Mac. If Office finds previously created custom dictionaries in any location other than the default dictionary location, you may be prompted for additional permissions to grant access to the files. If you see this message, simply select the file or files and click Select to grant access to custom dictionaries.
For more information about installing Office 2016 for Mac, including information about running Office 2016 for Mac on the same computer with Office for Mac 2011, see Install Office on your PC or Mac.