append to dictionary value python code example
Example 1: how to append to a dictionary in python
d = {'a': 1, 'b': 2}
print(d)
d['a'] = 100 # existing key, so overwrite
d['c'] = 3 # new key, so add
d['d'] = 4
print(d)
Example 2: append dictionary python
>>> d1 = {1: 1, 2: 2}
>>> d2 = {2: 'ha!', 3: 3}
>>> d1.update(d2)
>>> d1
{1: 1, 2: 'ha!', 3: 3}
Example 3: append to dictionary python
# Append to Dictionary in Python
# Let's say we had the following dictionary:
languages = {'#1': "Python", "#2": "Javascript", "#3": "HTML"}
# There are two ways to add a key-and-value set to this dictionary
# Number 1: By .update() method
languages.update({"#4": "C#"}) # Adds a #4 key-and-value set
#--------------------------------------------
# Number 2: The define-key method
# This is the easier one
languages['#4'] = 'C#'
# ^^ Just updates a key of #4 to C#, or adds it in this case