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
  • Tkinter GUI tutorial python – how to clean excel data Python
  • What does the … operator do in Julia? julia
  • How to Create an XML file from Excel using Python Python
  • Welcome to Data Analytics Ireland Livestream
  • What is Javascript? Javascript
  • Free ways to Extract Data from Files Livestream
  • How to Pass Python Variables to Javascript Javascript
  • How to Group By in a Select Statement SQL

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: cannot unpack non-iterable int object Python
  • Tkinter GUI tutorial python – how to clean excel data Python
  • Regular expressions python Python
  • Python Dictionary Interview Questions Python
  • How to remove characters from an imported CSV file Python Tutorial
  • How to Compare Column Headers in CSV to a List in Python CSV

Select your language!

  • English
  • Español
  • Português
  • Français
  • Italiano
  • Deutsch
  • data cleansing in a business environment Articles
  • YouTube channel lists – Python Lists Python Lists
  • Recursion Definition
  • What Is An Array In Python? array
  • How to add a date when a record is created SQL
  • What are Local and Global Variables in Python? Python
  • How Would You Change The Name Of a Key in a Python Dictionary Python
  • What is a Unique key in SQL? SQL

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