How to Initialize a Dictionary in Python [8 Ways]

Here are eight ways to initialize a dictionary in Python:

  1. Using literals
  2. Using dict() Constructor
  3. Using fromKeys()
  4. Using Dictionary Comprehensions
  5. Using zip() and dict Constructor
  6. Using tuple
  7. Using setdefault()
  8. Using __setitem__()

Method 1: Using literals

This is the easiest way to initialize a dictionary with key-value pairs as literals enclosed in curly braces.

Visual Representation

Visual Representation of Python Initialize a Dictionary using literals

Example

# Initializing an empty dictionary
empty_dict = {}
print(empty_dict)

# Dictionary with initial key-value pairs
my_dict = {
 "India": "Delhi",
 "USA": "Washington, D.C.",
 "France": "Paris",
 "Japan": "Tokyo"
}
print(my_dict)

Output

{}
{'India': 'Delhi', 'USA': 'Washington, D.C.', 'France': 'Paris', 'Japan': 'Tokyo'}

Method 2: Using dict() Constructor

This method can be utilized with keyword arguments, where each argument represents a key-value pair in the dictionary.

Example

my_dict = dict(India="Delhi", USA="Washington, D.C.",
 France="Paris", Japan="Tokyo")

print(my_dict)

Output

{'India': 'Delhi', 'USA': 'Washington, D.C.', 'France': 'Paris', 'Japan': 'Tokyo'}

Method 3: Using fromkeys()

The fromKeys() method creates a new dictionary with keys from a given iterable, assigning the same shared value to all keys.

Visual Representation

Visual Representation of Using fromkeys()

Example

keys = {'Trump', 'Modi', 'Putin'}
value = 'Politician'

my_dict = dict.fromkeys(keys, value)

print(my_dict)

Output

{'Trump': 'Politician', 'Putin': 'Politician', 'Modi': 'Politician'}

Method 4: Using Dictionary Comprehensions

Dictionary comprehensions is particularly useful when you want to create a dictionary dynamically based on some logic or operation on an iterable.

Visual Representation

Visual Representation of Using Dictionary Comprehensions

Example

# Creating a dictionary with keys as numbers and values as their squares
my_dict = {x: x**2 for x in range(5)}

print(my_dict)

Output

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

Method 5: Using zip() and dict Constructor

The zip function pairs elements from the keys and values lists.

The dict constructor then takes these key-value pair and constructs a dictionary from them.

Example

keys = ['India', 'USA', 'France', 'Japan']
values = ['Delhi', 'Washington, D.C.', 'Paris', 'Tokyo']

my_dict = dict(zip(keys, values))
print(my_dict)

Output

{'India': 'Delhi', 'USA': 'Washington, D.C.', 'France': 'Paris', 'Japan': 'Tokyo'}

Method 6: Using tuple

You can also pass values to the dict() constructor using a list of tuples, with each tuple representing a key-value pair.

Example

my_dict = dict([('India', 'Delhi'), ('USA', 'Washington, D.C.'), 
('France', 'Paris'), ('Japan', 'Tokyo')])

print(my_dict)

Output

{'India': 'Delhi', 'USA': 'Washington, D.C.', 'France': 'Paris', 'Japan': 'Tokyo'}

Method 7: Using setdefault()

If you have a set of keys and you want to ensure they exist in the dictionary with default values, you can use setdefault().

Example

keys = ['India', 'USA', 'France', 'Japan']
default_value = None

my_dict = {}
for key in keys:
 my_dict.setdefault(key, default_value)

print(my_dict)

Output

{'India': None, 'USA': None, 'France': None, 'Japan': None}

Method 8: Using __setitem__()

Although it is not conventional way but you can directly call __setitem__() on a dictionary instance to set its key-value pairs.

my_dict = {}

my_dict.__setitem__('India', 'Delhi')
my_dict.__setitem__('USA', 'Washington, D.C.')
my_dict.__setitem__('France', 'Paris')
my_dict.__setitem__('Japan', 'Tokyo')

print(my_dict)

Output

{'India': 'Delhi', 'USA': 'Washington, D.C.', 'France': 'Paris', 'Japan': 'Tokyo'}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.