A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-dict-function/ below:

Python dict() Function - GeeksforGeeks

Python dict() Function

Last Updated : 08 Mar, 2025

dict() function in Python is a built-in constructor used to create dictionaries. A dictionary is a mutable, unordered collection of key-value pairs, where each key is unique. The dict() function provides a flexible way to initialize dictionaries from various data structures.

Example:

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

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

Explanation: In this example, we create a dictionary using keyword arguments. The keys One and Two are assigned the values "1" and "2" respectively.

Syntax of dict()

dict(iterable)

dict(mapping)

dict(**kwargs)

dict(iterable, **kwargs)

dict(mapping, **kwargs)

Parameter:

Return: The function returns a dictionary containing the specified key-value pairs.

Examples of dict()

Example 1: Creating dictionary using keyword arguments

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

print(d)

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

Explanation: dict() allows direct key-value pair assignment using the key=value syntax, where each key must be a valid Python identifier i.e., a string without quotes that follows variable naming rules.

Example 2: Creating deep-copy of the dictionary

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

# deep copy using dict
d_deep = dict(d)

# shallow copy without dict
d_shallow = d

# changing value in shallow copy will change d
d_shallow['a'] = 10
print(d)

# changing value in deep copy won't affect d
d_deep['b'] = 20
print(d)

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}

Explanation: A deep copy using dict(d) creates an independent dictionary d_deep, while d_shallow = d makes d_shallow reference the same object as d. Modifying d_shallow affects d, but changes in d_deep do not.

Example 3: Creating dictionary using iterables

Python
# creating a dictionary from a list of tuples
d = dict([('a', 1), ('b', 2), ('c', 3)], d=4)

print(d)

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

Explanation: The list of tuples [('a', 1), ('b', 2), ('c', 3)] contains key-value pairs that are used to create the dictionary. This means 'a' is assigned 1, 'b' is assigned 2, and 'c' is assigned 3. Additionally, we can directly add more key-value pairs using keyword arguments, like d=4.

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:

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4