Home Coding Dictionary – Update

Dictionary – Update

0
Dictionary – Update

# We start the week with a new DataStructure
# One of the first things we can do with a dictionary is add something to it.
# We can start with an empty dictionary and populate it, one piece by one piece.
# There are two ways, with the equal operator or with the .update() methods

# Just create an empty dictionary
# To create an empty dictionary we can assign no elements in curly brackets {}.
my_dict = {}

# And then we have to add some data in the dictionary, and
# you can do this with the equal operator.
# Example: dict[key] = [value]

my_dict["a"] = "Peter"
my_dict["b"] = "Anna"

# Or you can use the .update method
# The update method merges a dictionary with another dictionary
# Example: dict1.update(dict2)

my_secondDict = {"c":"Mark", "d":"Jeff", "e":"Grace"}
my_dict.update(my_secondDict)

# copy www.AEC.codes
OUT = my_dict
 
# NOTE
# In python 3.7 and newer dictionaries are ordered, it's
# mean that whatever you order the pairs in the dictionary,
# that order is maintained.
# Here in dynamo (with python 3.8, it seems that are not ordered

Dictionaries add and update values

If you try to update a dictionary or add a key:value pair to it, be sure that the key is not present in the dictionary.

Otherwise, you will update the value for the key already present and not add a new key:value pair.


One more way to create an empty dictionary

Another way to create a new dictionary is using the dict() function.

For example : myNewDict = dict()