How To Add To A Dictionary Python?


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.



Source link

Related News

Next Post

Discussion about this post

Subscribe To Our Newsletters

    Welcome Back!

    Login to your account below

    Retrieve your password

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

    Add New Playlist

    The Insight Post
    Privacy Overview

    This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.