Flexiple Logo
  1. Home
  2. Blogs
  3. Python
  4. How To Create A Dictionary In Python

How To Create A Dictionary In Python

Author image

Harsh Pandey

Software Developer

Published on Thu Mar 14 2024

Python dictionaries are incredibly versatile and useful data structures. They store data in key-value pairs and are crucial for efficient data organization and retrieval.

Understanding Python Dictionaries

A dictionary in Python is an unordered collection of data values, used to store data values like a map. Unlike other data types that hold only a single value as an element, dictionaries hold key-value pairs. Key-value pairs provide a useful way to store and retrieve data as a part of a pair.

Create A Dictionary

In Python, a dictionary can be created by placing a sequence of elements within curly {} braces, separated by a ‘comma’. Let us see a few examples to see how we can create a dictionary in Python.

Define A Dictionary With Items

To define a dictionary with items in Python, you initialize it with key-value pairs enclosed in curly braces. Dictionaries are versatile data structures that allow you to store pairs of elements - keys and values. Each key-value pair in a dictionary maps the key to its associated value.

Example

my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}
print(my_dict)

Output.

{'name': 'Alice', 'age': 30, 'city': 'New York'}

In this dictionary, 'name', 'age', and 'city' are keys, each associated with their respective values 'Alice', 30, and 'New York'. Note that keys are unique within a dictionary while values may not be. The values can be of any data type.

This method of dictionary creation is straightforward and commonly used in Python programming for tasks involving data storage and retrieval.

An Overview Of Keys And Values

In the process of creating a dictionary in Python, keys and values are fundamental components. A dictionary is a collection of key-value pairs, where each key is unique and is used to access its corresponding value.

Keys in a dictionary must be of an immutable type, such as strings, numbers, or tuples. Each key is linked to its value by a colon (:). Values, on the other hand, can be of any datatype and can even repeat.

Example

my_dict = {"name": "Alice", "age": 25, "city": "New York"}

In this dictionary, "name", "age", and "city" are keys, each pointing to values "Alice", 25, and "New York" respectively.

Accessing a value is straightforward. For instance, to get the age from my_dict, use.

print(my_dict["age"])

Output.

25

Modifying the value associated with a specific key is also simple. If we want to update the age to 26.

my_dict["age"] = 26
print(my_dict)

Output.

{'name': 'Alice', 'age': 26, 'city': 'New York'}

Remember, keys are unique in a dictionary. If you assign a new value to an existing key, the old value is overwritten.

Understanding keys and values is essential for efficiently creating and manipulating dictionaries in Python.

Built-in Dictionary Functions Methods In Python

Python offers a variety of built-in functions and methods for creating and manipulating dictionaries. Dictionaries in Python are collections of key-value pairs that are unordered, changeable, and indexed.

Creating a Dictionary

To create a dictionary, you can use curly braces {} or the dict() function.

# Using curly braces
my_dict = {'name': 'Alice', 'age': 25}
print(my_dict)  # Output: {'name': 'Alice', 'age': 25}

# Using dict() function
my_dict = dict(name='Alice', age=25)
print(my_dict)  # Output: {'name': 'Alice', 'age': 25}

Adding Elements

You can add new elements by assigning a value to a new key.

my_dict['email'] = 'alice@example.com'
print(my_dict)  # Output: {'name': 'Alice', 'age': 25, 'email': 'alice@example.com'}

Accessing Elements

Access elements using their keys.

print(my_dict['name'])  # Output: Alice

Modifying Elements

Modify elements by assigning a new value to an existing key.

my_dict['age'] = 30
print(my_dict)  # Output: {'name': 'Alice', 'age': 30, 'email': 'alice@example.com'}

Removing Elements

Use pop() to remove an item by key and return its value.

email = my_dict.pop('email')
print(email)  # Output: alice@example.com
print(my_dict)  # Output: {'name': 'Alice', 'age': 30}

Iterating Through a Dictionary

Use a for loop to iterate through the keys, values, or key-value pairs.

# Iterating through keys
for key in my_dict:
    print(key)  # Output: name, age

# Iterating through values
for value in my_dict.values():
    print(value)  # Output: Alice, 30

# Iterating through key-value pairs
for key, value in my_dict.items():
    print(key, value)  # Output: name Alice, age 30

Python dictionaries are powerful tools for data organization and manipulation. With these methods, you can efficiently create, modify, access, and iterate through dictionaries in your Python applications.

Related Blogs

Browse Flexiple's talent pool

Explore our network of top tech talent. Find the perfect match for your dream team.