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 Python Dictionary.
Headings of Contents
What is Dictionary?
Dictionary in Python is a data structure with a collection of elements. In python, the dictionary is an unordered, indexed, mutable or changeable data structure. Dictionary contains data in the form of key-value pairs as you can see below.
Student dictionary that contained information about a student.
student = {
"first_name": "John",
"last_name": "Doe",
"age": 26,
"country": "USA",
"email": "[email protected]"
}
What is the Python dict() function?
dict() function Python is a part of Python built-in functions, that is used to create the dictionary in Python. The 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 function in Python accepts ( **kwargs ) keyword argument parameters.
- keyword arguments:- Required. A keyword argument is a parameter whose value is determined by having a value assigned to that keyword name, for example, key1 = value, key2 = value, etc. Here key1 and key2 represent the name of the keyword argument. Keyword argument is always shorthand of kwargs.
Return Value
The return value of dict function in Python is always a Dictionary.
dict function examples:
Let’s explore the dict() function along with some examples. Here will try to create a Python dictionary in different-different ways so that you can use it more conventionally.
Example: Creating a dictionary using keyword argument
Create a simple dictionary using dict() function. Here, we are using the concept of keyword argument.
x = dict(name = 'Vishvajit', age = 22, country = 'India')
print(x)
Output
{'name': 'Vishvajit', 'age': 22, 'country': 'India'}
Example: Creating a dictionary using an object
In this example, we are creating a dictionary with the help of the object of the dict() function.
x = dict()
x['name'] = 'Vishvajit'
x['age'] = 22
x['country'] = 'India'
print(x)
Output
{'name': 'Vishvajit', 'age': 22, 'country': 'India'}
Example: Using dict() function with Python for loop
Suppose, we have two Python lists, One list has the name of the students, and the other one has the ages of the students that include in list one. Now we want to create a list of dictionaries and each dictionary in the list will contain the names and ages of the students.
Let’s see how can we do that.
import pprint students_names = ['Vishvajit', 'John', 'Harshita', 'Mayank', 'Pankaj', 'Rahul'] students_age = ['23', '21', '26', '20', '27', '23'] # zip object zip_object = zip(students_names, students_age) # empty list student_data = [] # iterate each item of zip object and convert into dict for name, age in zip_object: student_data.append(dict(name=name, age=age)) print("Students data:- ") pprint.pprint(student_data)
Output
Students data:-
[{'age': '23', 'name': 'Vishvajit'},
{'age': '21', 'name': 'John'},
{'age': '26', 'name': 'Harshita'},
{'age': '20', 'name': 'Mayank'},
{'age': '27', 'name': 'Pankaj'},
{'age': '23', 'name': 'Rahul'}]
Conclusion
In this tutorial, you have learned all about the dict function 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 tutorials.
Other Python built-in functions
- Python abs() function
- Python all() function
- Python any() function
- Python bin() function
- Python bool() function
- Python callable() function
- Python chr() function
For more information:- Click Here