How to Initialize a Dictionary with Values: A Complete Guide with Examples

How to Initialize a Dictionary with Values: A Complete Guide with Examples

You can initialize a dictionary with values by specifying key-value pairs within curly braces {}. Here's an example:

# Initializing a dictionary with values
my_dict = {'apple': 5, 'banana': 10, 'orange': 8}

# Printing the initialized dictionary
print(my_dict)

In this example, my_dict is initialized with three key-value pairs:

  • Key 'apple' with value 5

  • Key 'banana' with value 10

  • Key 'orange' with value 8

You can customize the keys and values according to your requirements.

Here are another five examples:

  1. Initializing a dictionary with strings as keys and integers as values:
my_dict = {'apple': 10, 'banana': 5, 'orange': 8}
  1. Initializing a dictionary with integers as keys and lists as values:
my_dict = {1: ['a', 'b', 'c'], 2: ['x', 'y', 'z'], 3: ['foo', 'bar']}
  1. Initializing a dictionary with tuples as keys and strings as values:
my_dict = {('John', 25): 'Engineer', ('Alice', 30): 'Manager', ('Bob', 35): 'Developer'}
  1. Initializing a dictionary with mixed types as keys and values:
my_dict = {1: 'one', 'two': 2, (3, 4): 'three four'}
  1. Initializing an empty dictionary and adding key-value pairs later:
my_dict = {}
my_dict['a'] = 1
my_dict['b'] = 2
my_dict['c'] = 3

These examples demonstrate different ways you can initialize a dictionary with values in Python.

If you want to iterate this dictionary no. 4 type then "my_dict = {1: 'one', 'two': 2, (3, 4): 'three four'}" using a loop. When you iterate over a dictionary, by default, it iterates over its keys. Here's how you can iterate over the my_dict dictionary and print both keys and values:

my_dict = {1: 'one', 'two': 2, (3, 4): 'three four'}

# Iterate over keys and values
for key in my_dict:
    value = my_dict[key]
    print(f"Key: {key}, Value: {value}")

Output:

Key: 1, Value: one
Key: two, Value: 2
Key: (3, 4), Value: three four

In this loop, key iterates over each key in the dictionary, and value is accessed using that key. You can then perform any operations you need with the key-value pairs inside the loop.

my_dict = {('John', 25): 'Engineer', ('Alice', 30): 'Manager', ('Bob', 35): 'Developer'}

You can loop through the dictionary my_dict and access both keys and values using a for loop. Since each key-value pair consists of a tuple as the key and a string as the value, you can unpack the key tuple to get the name and age of each person along with their respective job titles. Here's how you can do it:

my_dict = {('John', 25): 'Engineer', ('Alice', 30): 'Manager', ('Bob', 35): 'Developer'}

for (name, age), job in my_dict.items():
    print(f"{name} is {age} years old and works as a {job}.")

This will output:

John is 25 years old and works as a Engineer.
Alice is 30 years old and works as a Manager.
Bob is 35 years old and works as a Developer.

In this loop:

  • my_dict.items() returns a sequence of tuples, where each tuple contains a key-value pair.

  • We're using tuple unpacking in the loop to extract the name and age from the keys, and the job from the values.

  • Inside the loop, we're printing the formatted string for each person with their name, age, and job.

Did you find this article valuable?

Support LingarajTechhub All About Programming by becoming a sponsor. Any amount is appreciated!