Horje
How to Add New Line in Dictionary in Python

When working with dictionaries, you might find yourself in a situation where you need to add a new key-value pair. This process is straightforward, but understanding the nuances can be very helpful. In this article, we will explore how to add new lines (key-value pairs) to dictionaries in Python with practical examples.

Understanding Python Dictionaries

Before diving into adding new lines to dictionaries, let’s briefly review what a dictionary is. A Python dictionary is an unordered collection of items. Each item is a pair consisting of a key and a value. Dictionaries are defined using curly braces {}, with key-value pairs separated by commas.

Python
# Example of a dictionary
my_dict = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

Adding New Key-Value Pairs

Adding a new key-value pair to an existing dictionary is simple. You can use the assignment operator = to assign a value to a new key. If the key already exists, the value will be updated; otherwise, a new key-value pair will be created.

Method 1: Using Assignment

Python
# Existing dictionary
my_dict = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

# Adding a new key-value pair
my_dict["email"] = "[email protected]"

print(my_dict)

Output

{
"name": "John",
"age": 30,
"city": "New York",
"email": "[email protected]"
}

In this example, the new key "email" with the value "[email protected]" is added to my_dict.

Method 2: Using the update() Method

The update() method can be used to add multiple key-value pairs to a dictionary at once. This method takes another dictionary or an iterable of key-value pairs as an argument.

Python
# Existing dictionary
my_dict = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

# Adding new key-value pairs
my_dict.update({
    "email": "[email protected]",
    "phone": "123-456-7890"
})

print(my_dict)

Output

{
"name": "John",
"age": 30,
"city": "New York",
"email": "[email protected]",
"phone": "123-456-7890"
}

Method 3: Using dict Constructor with Keyword Arguments

You can also use the dict constructor with keyword arguments to add new key-value pairs to a dictionary. This method is useful when you want to add new items during the dictionary creation.

Python
# Creating a dictionary with additional key-value pairs
my_dict = dict(name="John", age=30, city="New York", email="[email protected]", phone="123-456-7890")

print(my_dict)

Output

{
"name": "John",
"age": 30,
"city": "New York",
"email": "[email protected]",
"phone": "123-456-7890"
}

Adding Key-Value Pairs Inside Loops

In some scenarios, you might need to add key-value pairs dynamically inside a loop. This is common when processing data or populating a dictionary based on certain conditions.

Python
# Initializing an empty dictionary
my_dict = {}

# Adding key-value pairs dynamically inside a loop
for i in range(5):
    key = f"item_{i}"
    value = i * 10
    my_dict[key] = value

print(my_dict)

Output

{
"item_0": 0,
"item_1": 10,
"item_2": 20,
"item_3": 30,
"item_4": 40
}








Reffered: https://www.geeksforgeeks.org


Python

Related
Building APIs using FastAPI with Django Building APIs using FastAPI with Django
Python Falcon - Deployment Python Falcon - Deployment
Parsel: How to Extract Text From HTML in Python Parsel: How to Extract Text From HTML in Python
Define Colors in a Figure Using Plotly Graph Objects and Plotly Express Define Colors in a Figure Using Plotly Graph Objects and Plotly Express
Authentication and Authorization with FastAPI Authentication and Authorization with FastAPI

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
19