Python dict() Function



The Python dict() function is used to create a new dictionary. A dictionary is a data structure that stores a collection of key-value pairs. Each key in the dictionary is unique and maps to a specific value. It is a mutable (changeable) and unordered structure.

Dictionaries are defined using curly braces {}, and each key-value pair is separated by a "colon". For example, you can use a dictionary to store information like names and corresponding ages.

Syntax

Following is the syntax of Python dict() function −

dict(iterable)

Parameters

This function accepts a list of tuples as a parameter where each tuple represents a key-value pair.

Return Value

This function returns a new dictionary object.

Example 1

In the following example, we are using the dict() function with keyword arguments to create a dictionary "person" with each key associated with a specific value −

person = dict(name="Alice", age=30, city="New York")
print('The dictionary object obtained is:',person)

Output

Following is the output of the above code −

The dictionary object obtained is: {'name': 'Alice', 'age': 30, 'city': 'New York'}

Example 2

Here, we are using the dict() function to convert the list of tuples "data_tuples" into a dictionary by associating each tuple's first element as the key and the second element as the value −

data_tuples = [("a", 1), ("b", 2), ("c", 3)]
data_dict = dict(data_tuples)
print('The dictionary object obtained is:',data_dict)

Output

Output of the above code is as follows −

The dictionary object obtained is: {'a': 1, 'b': 2, 'c': 3}

Example 3

In here, we are using the dict() function along with a list comprehension to merge dictionaries in the "data_list" into a single dictionary −

data_list = [{'name': 'Alice'}, {'age': 25}, {'city': 'London'}]
data_dict = dict((key, value) for d in data_list for key, value in d.items())
print('The dictionary object obtained is:',data_dict)

Output

The result obtained is as shown below −

The dictionary object obtained is: {'name': 'Alice', 'age': 25, 'city': 'London'}

Example 4

In this case, we use the dict() function in conjunction with the zip() function to pair up elements from the keys and values lists to create a dictionary −

keys = ["name", "age", "city"]
values = ["Bob", 28, "Paris"]
person_dict = dict(zip(keys, values))
print('The dictionary object obtained is:',person_dict)

Output

Following is the output of the above code −

The dictionary object obtained is: {'name': 'Bob', 'age': 28, 'city': 'Paris'}

Example 5

In this example, we first initialize an empty dictionary "empty_dict" using the dict() function. Subsequently, we populate the dictionary by adding key-value pairs −

empty_dict = dict()
empty_dict["name"] = "John"
empty_dict["age"] = 30
empty_dict["city"] = "Berlin"
print('The dictionary object obtained is:',empty_dict)

Output

The result produced is as follows −

The dictionary object obtained is: {'name': 'John', 'age': 30, 'city': 'Berlin'}
python_type_casting.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy