In python, there are multiple ways to add keys or values to dictionary.

You can use assignment operator to add key to dictionary.

dic[‘x’]=1

There are other ways to add to dictionay as well in python.

dic.update({‘x’:1})
# OR
dic.update(dict(x=1))
# OR
dic.update(x=1)

Example:

Output:

Before: {‘x’: 1, ‘y’: 2, ‘z’: 3}
After: {‘x’: 1, ‘y’: 2, ‘z’: 3, ‘p’: ’24’}

## Add multiple keys to dictionary

If you want to add multiple keys in one time, you can use update method.

Output:

Before: {‘x’: 1, ‘y’: 2, ‘z’: 3}
After: {‘x’: 1, ‘y’: 2, ‘z’: 3, ‘a’: 4, ‘b’: 5}

import_contacts

## Related Posts

• 27 December

### Create Array of All NaN Values in Python

Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63c5c91cbb740313176192/] [crayon-63c5c91cbb746171247867/] We used numpy.empty() […]

• 27 December

### Call Function from Another Function in Python

Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63c5c91cbbe56859480648/] [crayon-63c5c91cbbe5d286857325/] To call a function, […]

• 27 December

### [Fixed] NameError Name ‘unicode’ is Not Defined in Python

Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63c5c91cbc490876168710/] [crayon-63c5c91cbc496232149227/] Unicode is a computing industry standard that ensures that text from most of […]

• 26 December

### Repeat List N Times in Python

Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

• 26 December

### Get First Day of Next Month in Python

Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

• 25 December

### Convert String Array to Int Array in Python

Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.