Function Details: dict

Description


Creates a new dictionary.


Extended Description


The dict() function creates a new dictionary. It can be called with various types of arguments: no argument (to create an empty dictionary), a mapping object, an iterable of key/value pairs, or keyword arguments. Dictionaries store key-value pairs and are mutable, ordered (as of Python 3.7), and do not allow duplicate keys.


Exceptions


  • TypeError: If the iterable contains elements that are not key-value pairs
  • ValueError: If a key is specified multiple times in the iterable

Read More about dict from Python Documentation

Function Signature


dict(iterable: Optional[Iterable] = None, **kwargs: Any) -> dict

Module: builtins

Class: dict

Parameters


iterable: An optional iterable of key-value pairs to initialize the dictionary. **kwargs: Keyword arguments to be added as key-value pairs to the dictionary.


Parameter List


  • iterable: Optional[Iterable]
  • **kwargs: Any

Return


Returns a new dictionary object.


Return Type


dict

Output

Explanation

This example demonstrates various ways to create dictionaries using the dict() function.