AppDividend
Latest Code Tutorials

Python dict() example | Python dict() Function

0

Python dict() method constructs a dictionary in python. Dictionary means a set of data that can be unordered, changeable, and which is 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.

Python dict()

Python provides another composite datatype called a dictionary, which is similar to a list in that it is a collection of objects.

Python dict() Syntax

dict(keyword arguments)

Keyword: Is a variable which holds the value of the argument

Arguments: Argument is the value of a keyword

 For example,

x=dict(name=”Appdividend” , website =”appdividend.com”)

Here, name and website are keyword and “Appdividend”, “appdividend.com” are arguments.

Here are the different forms of dict() constructors.

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

The keyword argument is an argument 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. **kwarg lets you take the 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 the unique and immutable.

If no parameters are given in the dict()function, an empty dictionary is created.

Now let’s go through each type of dict() constructors we mentioned earlier.

#Example: How to create Dictionary in Python

Dictionaries are Python’s implementation of the data structure that is more generally known as an associative array.

The dictionary consists of the collection of key-value pairs. Each key-value pair maps the key to its associated value.

You can define the dictionary by enclosing the comma-separated list of key-value pairs in curly braces ({}). A colon (:) separates each key from its associated value.

See the following code.

# 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))

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])

In this case, 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]))

In this case, 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:

  1. Both are mutable.
  2. Both are dynamic. They can grow and shrink as needed.
  3. 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:

  1. List items are accessed by their position in the list, via indexing.
  2. Dictionary items are accessed via keys.

Finally, Python dict() example | Python dict() Function Tutorial is over.

Recommended Posts

Python Dictionary get()

Convert Python dict to json

Python Dictionary fromKeys()

Python Dictionary keys()

Python Dictionary pop()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.