Table of Contents
The dict() constructor creates a dictionary in Python.
Different forms of dict()
constructors are:
class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg)
Note: **kwarg
let you take an arbitrary number of keyword arguments.
A keyword argument is an argument preceded by an identifier (eg. name=
). Hence, the keyword argument of the form kwarg=value
is passed to dict()
constructor to create dictionaries.
dict()
doesn’t return any value (returns None
).
1. Example 1: Create Dictionary Using keyword arguments only
numbers = dict(x=5, y=0) print('numbers =', numbers) print(type(numbers)) empty = dict() print('empty =', empty) print(type(empty))
Output
numbers = {'y': 0, 'x': 5} <class 'dict'> empty = {} <class 'dict'>
2. Example 2: Create Dictionary Using Iterable
# keyword argument is not passed numbers1 = dict([('x', 5), ('y', -5)]) print('numbers1 =',numbers1) # keyword argument is also passed numbers2 = dict([('x', 5), ('y', -5)], z=8) print('numbers2 =',numbers2) # zip() creates an iterable in Python 3 numbers3 = dict(dict(zip(['x', 'y', 'z'], [1, 2, 3]))) print('numbers3 =',numbers3)
Output
numbers1 = {'y': -5, 'x': 5} numbers2 = {'z': 8, 'y': -5, 'x': 5} numbers3 = {'z': 3, 'y': 2, 'x': 1}
3. Example 3: Create Dictionary Using Mapping
numbers1 = dict({'x': 4, 'y': 5}) print('numbers1 =',numbers1) # you don't need to use dict() in above code numbers2 = {'x': 4, 'y': 5} print('numbers2 =',numbers2) # keyword argument is also passed numbers3 = dict({'x': 4, 'y': 5}, z=8) print('numbers3 =',numbers3)
Output
numbers1 = {'x': 4, 'y': 5} numbers2 = {'x': 4, 'y': 5} numbers3 = {'x': 4, 'z': 8, 'y': 5}
Recommended Reading: Python dictionary and how to work with them.
Related posts:
Python Program to Find Sum of Natural Numbers Using Recursion
Python Set union()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Python Program to Create Pyramid Patterns
Python frozenset()
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Tuple count()
Python Program to Find the Sum of Natural Numbers
Python del Statement
Python Program to Check If a List is Empty
Python Program to Get File Creation and Modification Date
Python Matrices and NumPy Arrays
Python Program to Display Powers of 2 Using Anonymous Function
Python Modules
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Get the Last Element of the List
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Find Numbers Divisible by Another Number
Python Global, Local and Nonlocal variables
Python Program to Represent enum
Python Dictionary
Python type()
Python String isdigit()
Python Set discard()
Python @property decorator
Python String casefold()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Set intersection_update()
Python __import__()
Python Program to Check if a Number is Positive, Negative or 0