The Wayback Machine - https://web.archive.org/web/20241130215936/https://www.geeksforgeeks.org/python-dict-function/
Open In App

Python dict() Function

Last Updated : 03 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A dictionary is a mutable data structure i.e. the data in the dictionary can be modified. A dictionary is an indexed data structure i.e. the contents of a dictionary can be accessed by using indexes, here in the dictionary, the key is used as an index. Here, the dict() function is used to create a new dictionary or convert other iterable objects into a dictionary. In this article, we will learn more about Python dict() function.

Python dict() Syntax

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

Parameters:

kwargs: It is a keyword argument.terable.

iterable: An iterable containing keyword arguments

mapping: It is another dictionary.

dict() Function in Python

dict() function is used to create a new dictionary or convert other iterable objects into a dictionary. Dictionaries in Python are collections of key-value pairs, and the dict() function allows us to create them in various ways.

Python dict() Function is used to create a Python dictionary, a collection of key-value pairs.

Python
dict(One = "1", Two = "2")

Output:

{'One': '1', 'Two': '2'}

Example 1: Creating dictionary using keyword arguments

We can pass keyword arguments as a parameter with the required values that will be keys and values of the dictionary.

Syntax:

dict(**kwarg)

Python
# passing keyword arguments to dict() method
myDict = dict(a=1, b=2, c=3, d=4)

print(myDict)

Output:

{'a': 1, 'b': 2, 'c': 3, 'd': 4}

Example 2: Creating deep-copy of the dictionary using dict()

Creating a new instance (deep copy) of dictionary using dict().

Syntax:

dict(mapping)

Python
main_dict = {'a': 1, 'b': 2, 'c': 3}

# deep copy using dict
dict_deep = dict(main_dict)

# shallow copy without dict
dict_shallow = main_dict

# changing value in shallow copy will change main_dict
dict_shallow['a'] = 10
print("After change in shallow copy, main_dict:", main_dict)

# changing value in deep copy won't affect main_dict
dict_deep['b'] = 20
print("After change in deep copy, main_dict:", main_dict)

Output:

After change in shallow copy, main_dict: {'a': 10, 'b': 2, 'c': 3}
After change in deep copy, main_dict: {'a': 10, 'b': 2, 'c': 3}

Example 3: Creating dictionary using iterables

The keys and values can be passed to dict() in form of  iterables like lists or tuples to form a dictionary and keyword arguments can also be passed to dict().

Syntax:

dict(iterable, **kwarg)

Python
# A list of key value pairs is passed and
# a keyword argument is also passed
myDict = dict([('a', 1), ('b', 2), ('c', 3)], d=4)

print(myDict)

Output:

{'a': 1, 'b': 2, 'c': 3, 'd': 4}


Python dict() Function – FAQs

Why use dict in Python?

A dict (dictionary) is used for storing data in key-value pairs. It is highly efficient for lookups, insertions, and deletions due to its underlying hash table implementation. Dictionaries are versatile for managing data with unique identifiers and are dynamically resizable.

What does dict.items() do?

The dict.items() method returns a view object that displays a list of the dictionary’s key-value pairs. This view object is dynamic, meaning it updates when the dictionary changes.

Example:

my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
items = my_dict.items()
print(items) # Output: dict_items([('name', 'Alice'), ('age', 30), ('city', 'New York')])

Is dict() and {} the same?

No, dict() and {} are different ways to create dictionaries:

  • dict(): A constructor that can take keyword arguments, a list of tuples, or another dictionary.
  • {}: A literal syntax used to create an empty dictionary or one with specified key-value pairs.

Example:

# Using dict() to create a dictionary
dict1 = dict(name='Alice', age=30)
# Using {} to create a dictionary
dict2 = {'name': 'Alice', 'age': 30}

Can we use dict() method to copy a dictionary?

Yes, you can use the dict() constructor to create a shallow copy of an existing dictionary. This method copies the dictionary but does not clone nested objects.

Example:

original_dict = {'name': 'Alice', 'age': 30}
copied_dict = dict(original_dict)
print(copied_dict) # Output: {'name': 'Alice', 'age': 30}

What type is dict.keys() in Python?

The dict.keys() method returns a view object of type dict_keys, which displays a dynamic view of all the dictionary’s keys. It reflects changes made to the dictionary.

Example:

my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
keys = my_dict.keys()
print(type(keys)) # Output: <class 'dict_keys'>
print(keys) # Output: dict_keys(['name', 'age', 'city'])


Previous Article
Next Article

Similar Reads

three90RightbarBannerImg