Active7 days ago

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.

Manualkmario23
23.2k6 gold badges76 silver badges88 bronze badges
lfaraonelfaraone
19.1k15 gold badges45 silver badges67 bronze badges

13 Answers

Nick Larsen
14.8k6 gold badges55 silver badges87 bronze badges
Paolo BergantinoPaolo Bergantino
397k76 gold badges496 silver badges427 bronze badges

To add multiple keys simultaneously:

For adding a single key, the accepted answer has less computational overhead.

jeremysprofile
2,8762 gold badges11 silver badges29 bronze badges
user142911

I feel like consolidating info about Python dictionaries:

Creating an empty dictionary

Creating a dictionary with initial values

Inserting/Updating a single value

Inserting/Updating multiple values

Creating a merged dictionary without modifying originals

Deleting items in dictionary

Check if a key is already in dictionary

Iterate through pairs in a dictionary

Create a dictionary from 2 lists

Creating a merged dictionary without modifying originals

Feel free to add more!

Yugal JindleYugal Jindle
24.6k36 gold badges113 silver badges182 bronze badges

Yes 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, {}:

Best Practice 1: Subscript notation

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:

Best Practice 2: The update method - 2 ways

We 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.

Aaron HallAaron Hall
203k58 gold badges319 silver badges275 bronze badges
Jason CreightonJason Creighton
14.3k8 gold badges31 silver badges32 bronze badges

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

Add Manual Entry In Mac Dictionary Free

before adding entries.

user8909115
htmlfarmerhtmlfarmer
1,6294 gold badges20 silver badges34 bronze badges

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

Georgy
3,0964 gold badges20 silver badges33 bronze badges
Colonel PanicColonel Panic
85.9k65 gold badges314 silver badges400 bronze badges
kiriloffkiriloff
15k30 gold badges110 silver badges192 bronze badges

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

Community
nobarnobar
29.5k10 gold badges97 silver badges107 bronze badges

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

campetersoncampeterson
2,4411 gold badge19 silver badges25 bronze badges

So 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 Ekoka
10.2k6 gold badges53 silver badges71 bronze badges

If 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.

Burak ÖzdemirBurak Özdemir

first to check whether the key already exists

then you can add the new key and value

Agus MathewAgus Mathew

protected by MarcinSep 20 '13 at 19:06

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?

Not the answer you're looking for? Browse other questions tagged pythondictionarylookup or ask your own question.

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

  1. In the document, select the word that you want to add.

  2. 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.

  3. In the Add words to menu, select the dictionary you want to add your word to.

  4. 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.

  1. Make sure that a document is open.

    If no document is open, the next step won't work.

  2. On the Word menu, click Preferences.

  3. Under Authoring and Proofing Tools, click Spelling and Grammar.

  4. Under Spelling, click Dictionaries.

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

  6. 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.

  7. Click Save, and then click OK again to close the Spelling and Grammar dialog box.

Import a custom dictionary in Word

Add Manual Entry In Mac Dictionary Download

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.

  1. Make sure that a document is open.

    If no document is open, the next step won't work.

  2. On the Word menu, click Preferences.

  3. Under Authoring and Proofing Tools, click Spelling and Grammar.

  4. Under Spelling, click Dictionaries.

  5. 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.

  1. Make sure that a document is open.

    If no document is open, the next step won't work.

  2. On the Word menu, click Preferences.

  3. Under Authoring and Proofing Tools, click Spelling and Grammar.

  4. Under Spelling, click Dictionaries.

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

  6. 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.

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

  1. Make sure that a document is open.

    If no document is open, the next step won't work.

  2. On the Word menu, click Preferences.

  3. Under Authoring and Proofing Tools, click Spelling and Grammar.

  4. Under Spelling, click Dictionaries.

  5. 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.

See also