Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • 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
  • how to select columns with SQL SQL
  • How to remove unwanted characters Python Data Cleansing
  • How to delete a key from a Python dictionary Python
  • How can packages be managed in Julia? Julia programming
  • What is The Julia Programming Language Julia programming
  • What is an Algorithm? machine learning
  • TypeError: expected str, bytes or os.PathLike object, not DataFrame Python

How To Add Values to a Python Dictionary

Posted on October 22, 2022March 12, 2023 By admin

Estimated reading time: 0 minutes

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.

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

  • TypeError object of type ‘int’ has no len() Python
  • TypeError: cannot unpack non-iterable int object Python
  • how to create charts in Tkinter Python
  • How To Delete a Set of Keys From a Python Dictionary python dictionaries
  • How To Compare CSV Files for Differences CSV
  • How to use zip() function in Python Python
  • IndexError: single positional indexer is out-of-bounds Index Error
  • What is the r programming language R Programming
  • YouTube channel lists – Python Data Cleansing Python Data Cleansing
  • What is GITHUB, and should I use it? github
  • How to add a date when a record is created SQL
  • Deleting table records with SQL SQL
  • What is the difference between DROP and TRUNCATE in SQL? SQL
  • How to pass by reference or pass by value in Python Python

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