Skip to content

Make Dictionary Python | Basics

  • by

Making a dictionary is as simple as just do place elements inside curly braces {} separated by commas. An element has a key and a corresponding value that is expressed as a pair (key: value).

Empty Dictionary:

# empty dictionary
my_dict = {}

Example Make a Python Dictionary

Simple example code you can also create a dictionary directly with initial values:

employees = {
    "1": "John",
    "2": "Nell",
    "3": "Tim"



Make Dictionary Python

You can also make a dictionary using the dict() function.

For example, making a dictionary with integer keys.

my_dict = dict({1: 'apple', 2: 'ball'})


Output: {1: ‘apple’, 2: ‘ball’}

You can access the values in a dictionary using the keys:

# Accessing values in the dictionary
print(my_dict['apple'])   # Output: 3

You can also check if a key exists in a dictionary:

# Checking if a key exists
print('apple' in my_dict)   # Output: True
print('mango' in my_dict)   # Output: False

More Examples

# dictionary with mixed keys
my_dict = {'name': 'John', 1: [2, 4, 3]}

# from sequence having each item as a pair
my_dict = dict([(1, 'apple'), (2, 'ball')])


{‘name’: ‘John’, 1: [2, 4, 3]}
{1: ‘apple’, 2: ‘ball’}

These are some basic operations you can perform with dictionaries in Python. Dictionaries are incredibly versatile and allow for efficient lookups and modifications based on the keys.

Comment if you have any doubts or suggestions on this Python dictionary topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *