Python dictionary dict() method [With Examples]

In this Python tutorial, we will discuss everything about the Python Dictionary dict() method. I will also show you, the syntax of dict() dictionary method and how to use the dictionary dict() method with a few examples.

Python dictionary dict() method

The dict() method in Python is a built-in function used for creating dictionaries. A dictionary in Python is a mutable, unordered collection of key-value pairs.

Each key in a dictionary must be unique, and each key is associated with a value. This data structure is very useful for storing and managing data that can be neatly organized as pairs.

The general syntax of the dict() method is as follows:

dict(**kwargs)
dict(mapping, **kwargs)
dict(iterable, **kwargs)

dict() method in Python Dictionary Examples

Let’s dive into some examples to better understand the Python Dictionary dict() method in action:

Example#1 Basic Usage

The simplest way to use the Python dict() function is with keyword arguments, where the argument’s name is treated as the key and the argument’s value as the value:

my_dict = dict(name='John Doe', age=30, profession='Engineer')

print(my_dict) 

In this example, we’re creating a python dictionary using the dict() function with keyword arguments. Here, 'name', 'age', and 'profession' are the keys (or identifiers), and 'John Doe', 30, and 'Engineer' are their corresponding values. The dict() function then creates a python dictionary from these arguments.

Output:

Python dictionary dict method
Python dictionary dict method

Example#2 Iterable Key-Value Pairs

The dict() method can also create a Python dictionary from an iterable containing key-value pairs. Each pair in the iterable should be another iterable with exactly two objects – the first one will be treated as the key, and the second one as the value:

my_dict = dict([('name', 'John Doe'), ('age', 30), ('profession', 'Engineer')])

print(my_dict) 

Here, the iterable is a Python list of tuples, where each Python tuple contains a pair of key-value data. The Python dict() function treats the first item of the tuple as a key and the second item as its value, creating a Python dictionary out of these pairs.

READ:  How to convert a list to DataFrame in Python [9 ways]

Output:

Python dictionary dict method example
Python dictionary dict method example

Example#3 From Another Dictionary

The dict() function can also be used to create a Python dictionary from another Python dictionary. In such cases, it essentially creates a copy of the original dictionary:

original_dict = {'name': 'John Doe', 'age': 30, 'profession': 'Engineer'}
my_dict = dict(original_dict)

print(my_dict) 

In this example, we’re creating a new Python dictionary from an existing one using the dict() function. The function takes an existing dictionary as an argument and makes a copy of it. This is useful when you need to duplicate a dictionary without altering the original one.

Output:

dict method in Python Dictionary Examples
dict method in Python Dictionary Examples

Conclusion

The Python dictionary dict() the function offers a flexible way to create dictionaries. Whether we are working with keyword arguments, pairs of data, or existing dictionaries, dict() provides a concise way to generate dictionaries to suit our needs.

You may also like:

  • Python dictionary len() method
  • Python dictionary fromkeys() method