Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • YouTube channel lists – Tuples Python Tuples
  • YouTube channel lists – Python working with files Python working with files
  • how to create an instance of a class class
  • How To Pass Data Between Functions Python Functions
  • How Would You Change The Name Of a Key in a Python Dictionary Python
  • ValueError: Columns must be same length as key exception handling
  • TypeError: ‘NoneType’ object is not iterable class
  • How To Join Tables In SQL SQL

How To Add Values to a Python Dictionary

Posted on October 22, 2022January 21, 2023 By admin No Comments on How To Add Values to a Python Dictionary

Estimated reading time: 1 minute

As part of our ongoing series about Python Interview questions, we discussed how you would create an empty dictionary.

In this posting, we are going to go through the different ways you could add values to your dictionary.

In the below code, we have created an empty dictionary for you.

https://youtube.com/shorts/cdx2D7cQMcE

So as we go down through lines 5 and 6, you can see that we have created two key names and assigned them values. This has the result of populating those key-value pairs to empty_dict1.

Now as you build out your Python logic, you may just want to update the dictionary by using empty_dict1.update(Add key-value pair here).

 

Finally, there is the option to just use an if statement that checks if the values you want to add exist already, and if not it adds them in.

#How do add values to a python dictionary
empty_dict1 = {}
empty_dict2 = dict()

empty_dict1['Key1'] = '1'
empty_dict1['Key2'] = '2'
print(empty_dict1)

#Example1 - #Appending values to a python dictionary
empty_dict1.update({'key3': '3'})
print(empty_dict1)

#Example2 - Use an if statement
if "key4" not in empty_dict1:
    empty_dict1["key4"] = '4'
else:
    print("Key exists, so not added")
print(empty_dict1)

Output:
{'Key1': '1', 'Key2': '2'}
{'Key1': '1', 'Key2': '2', 'key3': '3'}
{'Key1': '1', 'Key2': '2', 'key3': '3', 'key4': '4'}
Python, python dictionaries, Python Tutorial Tags:Python, python dictionaries

Post navigation

Previous Post: How To Create An Empty Dictionary In Python
Next Post: How to sort a Python Dictionary

Related Posts

  • How to Automate Testing With Python | unittest automation
  • How To Check For Unwanted Characters Using Loops With Python Python Data Cleansing
  • TypeError object of type ‘int’ has no len() Python
  • python constructor self and __init__explained Python
  • TypeError: ‘list’ object is not an iterator Python
  • TypeError: the first argument must be callable Python

Leave a Reply Cancel reply

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

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • how to select all records with SQL SQL
  • How to Add Formulas to Excel using Python numpy
  • how to remove unwanted characters from your data R Programming
  • Python tutorial: Create an input box in Tkinter Python
  • TypeError: cannot unpack non-iterable int object Python
  • How Would You Change The Name Of a Key in a Python Dictionary Python
  • Welcome to Data Analytics Ireland Livestream
  • ValueError: cannot convert float NaN to integer Null values

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT