| by Arround The Web | No comments

Python dict() Function

Python is a versatile language that has numerous built-in functions that make coding easier and faster. One of these functions is the “dict()” function that is used to create/construct a dictionary in Python. The dictionary is a data structure that stores/keeps the value in key-value pairs format.

This blog guide will provide a detailed overview of the Python “dict()” function using the below contents:

What is the Python “dict()” Function?

In Python, the “dict()” function is a built-in function that is utilized to create and retrieve the dictionary object with key-value pairs.

Syntax

dict(**kwarg)
dict(iterable, **kwarg)
dict(mapping, **kwarg)

 

Parameters

In the above syntax:

  • The “**kwarg” or keyword arguments specify the key-value pairs of the dictionary. These keyword arguments are separated by commas.
  • The “iterable” parameter specifies the iterable object in the form of key-value pairs.
  • The “mapping” parameter indicates the mapping object, such as another dictionary.

Return Value

The “dict()” function retrieves a new dictionary object with specified key-value pairs. When no argument is passed to the “dict()” function then an empty dictionary will be retrieved.

Example 1: Create/Construct a Dictionary in Python Utilizing the “dict()” Function

The following code is utilized to create/construct a dictionary in Python by using the “dict()” function. Here the “dict()” function takes the key along with its value as an argument and retrieves the dictionary object. We use the “type()” function to verify the dictionary object type:

dict_obj = dict(name = "Joseph", age = 22, country = "England")
print(dict_obj)
print(type(dict_obj))

 

The above code will retrieve the below dictionary object:

Example 2: Create a Dictionary Using Iterable in Python

We can also create a dictionary in Python by passing the key and its value in the form of a list of tuples into the “dict()” function. The “dict()” function takes these iterables and retrieves the dictionary object:

dict_obj = dict([('name', 'Joseph'), ('age', 22), ('country', 'England')])
print(dict_obj)
print(type(dict_obj))

 

The dictionary has been retrieved successfully:

We can also use the list of sets or sets of a tuple as an iterable object. Here is an example code that uses the “dict()” function to create a dictionary:

dict_obj = dict([{'name', 'Joseph'}, {'age', 22}, {'country', 'England'}])
print(dict_obj)
print(type(dict_obj), '\n')

dict_obj = dict({('name', 'Joseph'),('age', 22),('country', 'England')})
print(dict_obj)
print(type(dict_obj))

 

The dictionary has been retrieved successfully:

Example 3: Create a Dictionary Deep-Copy Using Mapping in Python

We can also create a dictionary deep copy using mapping in the “dict()” function in Python. The “dict()” function takes the original dictionary as an argument and creates a deep copy of the dictionary. Next, the shallow copy is created, and the element is modified. We can change or modify the shallow copy element value, but the deep copy element does not change:

dict1 = {'a': 1, 'b': 2, 'c': 3}
deep = dict(dict1)
shallow = dict1
shallow['a'] = 10
print("After Change in Shallow Copy:", dict1)
deep['b'] = 20
print("After Change in Deep Copy:", dict1)

 

According to the following output, the shallow copy and deep copy dictionary has been created successfully:

Conclusion

The “dict()” function is utilized to create and retrieve the key-value pairs dictionary object by taking the keywords arguments, iterable, or mapping objects. The iterable such as a list of tuples, sets of tuples, and others are passed to the “dict()” function to create a dictionary. We can also create a deep copy using the “dict()” function. This article demonstrates the “dict()” function through numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply