Menu Close

Python dict() Function

In this tutorial, you will learn about the Python dict() function.dict function in python is used to create the dictionary.
In our previous python built-in functions tutorial, we have seen Python complex() function to create a complex number.

To understand this example, you should have a basic knowledge of the Dictionary.

let’s see a little about the Dictionary.

What is Dictionary?

Dictionary in Python is a data structure with a collection of elements. In python, the dictionary is an unordered, indexed, and mutable or changeable data structure.
Read more about the dictionary from the python dictionary tutorial.

What is Python dict function?

Python dict function is a built-in function, that is used to create the dictionary in Python.dict function accepts parameters as keyword arguments and returns a dictionary.

Syntax

The syntax of dict function in python is:-

dict(keyword arguments)

Parameter

dict method in python accepts ( **kwargs ) keyword arguments parameters.

  • keyword arguments:- Required. As many keyword arguments, you like separated
    by commas, for example key = value, key = value, etc.

Return Value

Return value of dict function in Python is always python dictionary.

Python dict examples:

Here we will see some python examples to understand python dict function.

Example 1:

Create simple dictionary using dict() function.

x = dict(name = 'Vishvajit', age = 22, country = 'India')
print(x)

Output

{'name': 'Vishvajit', 'age': 22, 'country': 'India'}

Example 2:

Create the dictionary using object.

x = dict()
x['name'] = 'Vishvajit'
x['age'] = 22
x['country'] = 'India'
print(x)

Output

{'name': 'Vishvajit', 'age': 22, 'country': 'India'}

Conclusion

In this tutorial, you have learned all about the dict method in python to create the dictionary in Python. This is the best function to create the dictionary in Python easily.

If this article helped you, please keep visiting for further Python built-in functions tutorial.

Other Python built-in functions


For more information:- Click Here

Previous Article
Next Article

Related Posts

Leave a Reply