dict() in Python

Video Tutorial
FREE
Dictionary thumbnail
This video belongs to
Python and SQL for Data Science
8 modules
Certificate
Topics Covered

Overview

Dictionary in Python is an arbitrary collection of data values that can be used to store data like a map due to its key-value pair structure. This is unlike the other data types in Python, which can hold a single value. The dict() constructor in Python is one of the methods used to create a dictionary in Python.

Syntax of dict() in Python

Dictionary in Python consists of a key-value pair where the key maps to its associated value. A dictionary in Python is generally defined by enclosing a comma-separated list of key-value pairs in curly braces ({}). A key is separated from its value using a colon (:). You can understand this better by the syntax below:

Alternatively, a dictionary in Python can be constructed using Python's built-in dict() function. The argument of dict() contains a sequence of key-value pairs. If values are strings, they will be enclosed in inverted commas. Below is the syntax of the dict() function in Python:

The dict() constructor can take various forms, as shown below:

Parameters of dict() in python

The above syntax shows that the dict() constructor can take an arbitrary number of keyword arguments. kwarg represents this number.

A general argument is in the following format:

where the key is a name and value is mapped to this key.

Return Values of dict() in Python

dict() in Python does not return any value.

Example of dict() in Python

Let's consider a dictionary holding keys as numbers from 1; their values correspond to a fruit of the corresponding letter of the alphabet. Such a dictionary will be created as shown in the below code:

Code:

Output:

What is dict() in Python?

The dict() constructor in Python is used to create a dictionary in Python. Python dictionary is an unordered collection of pairs of key-value. Key-value structure of the dictionary makes it more optimized. This helps us easily retrieve values when the key is known.

To learn more dictionary in Python, check out this resource.

Restrictions on Dictionary Keys

A key in a dictionary in Python can be of various types, including integer, float, boolean, types, functions, etc. However, certain restrictions need to be followed by keys in a dictionary:

  1. A dictionary has unique keys, i.e., the same key cannot be repeated. This prevents the mapping of more than one value to the same key.
  2. If a key is repeated, the second occurrence is considered the latest, and the mapping will only be considered for that.
  3. The data type of a key in a dictionary in Python should have the immutable property. Thus, tuples can be dictionary keys, but lists and dictionaries cannot.

Restrictions on Dictionary Values

Unlike dictionary keys, there are no restrictions on Python dictionary values.

More Examples

Create Dictionary in Python Using keyword Arguments Only

Creating Dictionary in Python using arguments

Code:

Output:

Creating an Empty Dictionary in Python

Code:

Output:

Creating Dictionary in Python Using an Iterable

Code:

Output:

Creating a dictionary in Python using mapping

Code:

Output:

Python Dictionary Comprehension

Python Dictionary Comprehension is used to create a new dictionary in Python from an iterable. It consists of a (key: value) paired with a for the statement in curly braces. To get more clarity on this, follow the example below that helps create a dictionary that holds the table of 2.

Output:

A dictionary comprehension in Python can also contain nested for and if statements. This would help in further filtering items in the dictionary. The following example using the if statement helps in filtering values for only odd keys:

Output:

Conclusion

  • Dictionary in Python is a set of unordered key-value pairs.
  • dict() is a constructor that is used to define a dictionary in Python.
  • A dictionary key can only be present once, i.e., only one mapping is present for a key.
  • A dictionary key should be of an immutable data type.
  • A dictionary value can be of any type.