Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new dictionary in Python

I want to build a dictionary in Python. However, all the examples that I see are instantiating a dictionary from a list, etc . ..

How do I create a new empty dictionary in Python?

like image 289
leora Avatar asked Dec 08 '11 01:12

leora


People also ask

How do you create a new dictionary in Python?

In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by 'comma'. Dictionary holds pairs of values, one being the Key and the other corresponding pair element being its Key:value.

Can you create a dictionary of dictionaries in Python?

In Python, you can create a dictionary dict with curly brackets {} , dict() , and dictionary comprehensions.

Can you create multiple dictionary in Python?

Creating a Nested Dictionary In Python, a Nested dictionary can be created by placing the comma-separated dictionaries enclosed within braces.


2 Answers

Call dict with no parameters

new_dict = dict() 

or simply write

new_dict = {} 
like image 69
Jan Vorcak Avatar answered Nov 27 '22 02:11

Jan Vorcak


You can do this

x = {} x['a'] = 1 
like image 30
TJD Avatar answered Nov 27 '22 02:11

TJD