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

    Customer Support


    1251 Wilcrest Drive
    Houston, Texas
    77042 USA
    Call-832.795.1420
    e-mail – news@theinsightpost.com

    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