Python provides another composite datatype called a dictionary, similar to a list in that it is a collection of objects.
Python dict()
Python dict() is a built-in method that constructs a dictionary. A dictionary means a data set that can be unordered, changeable, and indexed. The dict() function creates a dictionary.
Dictionaries are mutable and unordered collections of key-value pairs where keys must be unique and hashable. They are also called associative arrays in other programming languages like php.
Syntax
dict(keyword arguments)
Keyword: Is a variable that holds the value of the argument
Arguments: Argument is the value of a keyword
For example,
x=dict(name= “Appdividend”, website = “appdividend.com”)
Here, the name and website are keywords “Appdividend”, and “appdividend.com” are arguments.
Here are the different forms of dict() constructors.
dict(**kwarg) dict([mapping, **kwarg]) dict([iterable, **kwarg])
The keyword argument is preceded by an identifier (e.g., name=). Hence, the keyword argument of a form kwarg=value is passed to a dict() constructor to create dictionaries.
kwarg: It’s the keyword argument and is optional in the function. The **kwarg lets you take an arbitrary number of keyword arguments.
mapping: It is also optional (Another dictionary).
iterable: It is also optional. An iterable is the collection of the key-value pairs where keys must be unique and immutable.
An empty dictionary is created if no parameters are given in the dict()function.
Now let’s go through each type of dict() constructor we mentioned earlier.
How to Create Dictionary in Python
You can create the dictionary by enclosing the comma-separated list of key-value pairs in curly braces ({}). A colon (:) separates each key from its associated value.
Dictionaries are Python’s implementation of the data structure, more generally known as an associative array.
The dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its associated value.
# Creating dictionary using keyword and agruments d1 = dict(name="Appdividend", website="appdividend.com") # printing the value of d1 print("Values of d1 are: ", d1) # creating an empty dictionary d2 = dict() print("Value of d2 is :", d2) # Creating dict using mapping d3 = dict({'Name': "Debasis", 'Roll': 3}) print("Values of d3 is: ", d3) # Mapping without using dict d4 = {'Name': 'Debasis', 'Roll': 3} print("Value of d4 is: ", d4) # Creating an iterable dictionary # zip() creates an iterable dictionary in Python3 ite = dict(dict(zip(['Name', 'Roll', 'Section'], ["Debasis", 3, "CSE21"]))) print("The vlaue of iterable dict is: ", ite)
See the output.
Values of d1 are: {'name': 'Appdividend', 'website': 'appdividend.com'} Value of d2 is : {} Values of d3 is: {'Name': 'Debasis', 'Roll': 3} Value of d4 is: {'Name': 'Debasis', 'Roll': 3} The value of iterable dict is: {'Name': 'Debasis', 'Roll': 3, 'Section': 'CSE21'}
Create dictionary using keyword arguments(dict(**kwarg))
You can create a dictionary and fill the values using (dict(**kwarg)) arguments.
First, let’s create an empty dictionary.
# app.py print(dict())
See the output.
➜ pyt python3 app.py {} ➜ pyt
Now, let’s pass the keyword arguments.
# app.py print(dict(el=11, k=21))
See the output.
➜ pyt python3 app.py {'el': 11, 'k': 21} ➜ pyt
Create dictionary using mapping: dict([mapping, **kwarg])
A dictionary is created with the same key-value pairs as mapping objects.
# app.py print(dict({'el': 11, 'k': 21})) print(dict({'mike': 12, 'dustin': 18}, sadie=19))
See the output.
➜ pyt python3 app.py {'el': 11, 'k': 21} {'mike': 12, 'dustin': 18, 'sadie': 19} ➜ pyt
Create dictionary using iterables(dict([iterable, **kwarg]))
Each element of the iterable must be iterable with two objects. The first object becomes a key, and the following object becomes a value for that corresponding key.
# app.py print(dict([('el', 11), ('k', 21)])) print(dict([('mike', 12), ('dustin', 18)], sadie=19))
See the output.
➜ pyt python3 app.py {'el': 11, 'k': 21} {'mike': 12, 'dustin': 18, 'sadie': 19} ➜ pyt
Python Dictionary and List
Dictionaries and lists share the following characteristics:
- Both are mutable.
- Both are dynamic. They can grow and shrink as needed.
- Both can be nested. A list can contain another list. A dictionary can contain another dictionary. A dictionary can also contain an index and vice versa.
Dictionaries differ from Python lists primarily in how elements are accessed:
- List items are accessed by their position in the list via indexing.
- Dictionary items are accessed via keys.
That’s it.
Thanks for the guide