How To Add To A Dictionary Python?

[ad_1]

53 / 100

In Python, you can add a key-value pair to a dictionary using the following syntax:

Here’s an example:

# Create an empty dictionary

# Add key-value pairs to the dictionary


my_dict['grapes'] = 3
my_dict['banana'] = 5
my_dict['apple'] = 2

# Print the dictionary

Output

 

{‘grapes’: 3, ‘banana’: 5, ‘apple’: 2}

In this example, we created an empty dictionary my_dict and then added three key-value pairs to it using the my_dict[key] = value syntax. The keys are strings (‘grapes’, ‘banana’, and ‘apple’), and the values are integers (3, 5, and 2). Finally, we printed the dictionary to verify that the key-value pairs were added correctly.

[ad_2]

Source link

Next Post

Leave a Reply

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

Subscribe To Our Newsletters

[contact-form-7 id=”551″ title=”Subscribe Now”]

Welcome Back!

Login to your account below

Retrieve your password

Please enter your username or email address to reset your password.