You can't .append() to a string because a string is not mutable. If you want your dictionary value to be able to contain multiple items, it should be a container type such as a list. The easiest way to do this is just to add the single item as a list in the first place.

if clientKey not in data:
    data[clientKey] = [ref]   # single-item list

Now you can data[clientkey].append() all day long.

A simpler approach for this problem is to use collections.defaultdict. This automatically creates the item when it's not there, making your code much simpler.

from collections import defaultdict

data = defaultdict(list)

# ... same as before up to your if

if clientkey in data and ref in data[clientkey]:
    print("That invoice already exists")
else:
    data[clientKey].append(ref)
Answer from kindall on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › append values to dictionary
r/learnpython on Reddit: Append values to Dictionary
June 8, 2021 -

I am currently using a while loop to loop and get certain values. I then want to append these values to a dictionary. Every loop through I want to append to two keys

General structure of the code: https://pastebin.com/p4hJcKR5

I have tried using:

dict[key] = value

dict.append(value)

And neither have worked, dict.append gives an error and dict[key] just sets the dictionary to the most recent iteration instead of iterating for all values. Any help would be appreciated.

🌐
DataCamp
datacamp.com › tutorial › python-dictionary-append
Python Dictionary Append: How to Add Key-Value Pairs | DataCamp
August 6, 2024 - Learn Python dictionary append techniques such as square bracket notation, the .update() method for bulk additions, and .setdefault() for conditional inserts.
🌐
GeeksforGeeks
geeksforgeeks.org › python › append-a-value-to-a-dictionary-python
Append a Value to a Dictionary Python - GeeksforGeeks
July 23, 2025 - For example, consider the dictionary ... 5, the dictionary will be updated accordingly. update() method is one of the most efficient way to append new key-value pairs to a dictionary....
🌐
Codecademy
codecademy.com › article › python-dictionary-append-how-to-add-items-to-dictionary
Python Dictionary Append: How to Add Items to Dictionary | Codecademy
Learn different ways to append and add items to a dictionary in Python using square brackets (`[]`), `update()`, loops, `setdefault()`, unpacking, and the union operator (`|`), with examples.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-add-to-dictionary
How to Add and Update Python Dictionaries Easily | DigitalOcean
October 16, 2025 - Just like the merge | operator, if a key exists in both dictionaries, then the update |= operator takes the value from the right operand. The following example demonstrates how to create two dictionaries, use the update operator to append the second dictionary to the first dictionary, and then print the updated dictionary: site = {'Website':'DigitalOcean', 'Tutorial':'How To Add to a Python Dictionary', 'Author':'Sammy'} guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}
🌐
Guru99
guru99.com › home › python › python dictionary append: how to add key/value pair
Python Dictionary Append: How to Add Key/Value Pair
August 13, 2025 - We can make use of the built-in function append() to add elements to the keys in the dictionary.
🌐
Career Karma
careerkarma.com › blog › python › python add to dictionary: a guide
Python Add to Dictionary: A Guide | Career Karma
December 1, 2023 - There is no add(), append(), or insert() method you can use to add an item to a dictionary in Python.
Find elsewhere
🌐
W3Schools
w3schools.com › python › python_dictionaries_add.asp
Python - Add Dictionary Items
Python Dictionaries Access Items Change Items Add Items Remove Items Loop Dictionaries Copy Dictionaries Nested Dictionaries Dictionary Methods Dictionary Exercises Code Challenge Python If...Else · Python If Python Elif Python Else Shorthand If Logical Operators Nested If Pass Statement Code Challenge Python Match ... Python Functions Python Arguments Python *args / **kwargs Python Scope Python Decorators Python Lambda Python Recursion Python Generators Code Challenge Python Range ... Matplotlib Intro Matplotlib Get Started Matplotlib Pyplot Matplotlib Plotting Matplotlib Markers Matplotlib Line Matplotlib Labels Matplotlib Grid Matplotlib Subplot Matplotlib Scatter Matplotlib Bars Matplotlib Histograms Matplotlib Pie Charts
🌐
freeCodeCamp
freecodecamp.org › news › add-to-dict-in-python
Adding to Dict in Python – How to Append to a Dictionary
February 28, 2023 - Let's see this in action. myDict = {'a': 1, 'b': 2} new_data = [('c', 3), ('d', 4)] myDict.update(new_data) print(myDict) ... In Python, a dictionary can be updated or made from scratch using the dict() constructor.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › python dictionary append: how to add key/value pair?
Python Dictionary Append: How To Add Key/Value Pair?
October 14, 2024 - Python dictionary append is simply used to add a key/value to the existing dictionary. The dictionary objects are mutable.
🌐
TechBeamers
techbeamers.com › python-dictionary
Python Dictionary – Create, Append, Update, Remove
November 30, 2025 - But the keys have a constraint to be of any immutable data type such as a string, a number, or a tuple. To learn more on this topic, please continue with the tutorial. Firstly, let’s see how to create a dictionary in Python. And secondly, we’ll see how to add/append elements to the Python ...
🌐
Finxter
blog.finxter.com › home › learn python blog › python dictionary append – 4 best ways to add key/value pairs
Python Dictionary Append - 4 Best Ways to Add Key/Value Pairs - Be on the Right Side of Change
February 11, 2023 - For example, dict1.update(dict2) will insert all key-value pairs of dict2 into dict1. You can add a single key:value pair to a Python dictionary by using the square bracket approach dict[key] = value.
Top answer
1 of 16
4456

You create a new key/value pair on a dictionary by assigning a value to that key

d = {'key': 'value'}
print(d)  # {'key': 'value'}

d['mynewkey'] = 'mynewvalue'

print(d)  # {'key': 'value', 'mynewkey': 'mynewvalue'}

If the key doesn't exist, it's added and points to that value. If it exists, the current value it points to is overwritten.

2 of 16
1364

I feel like consolidating info about Python dictionaries:

Creating an empty dictionary

data = {}
# OR
data = dict()

Creating a dictionary with initial values

data = {'a': 1, 'b': 2, 'c': 3}
# OR
data = dict(a=1, b=2, c=3)
# OR
data = {k: v for k, v in (('a', 1), ('b',2), ('c',3))}

Inserting/Updating a single value

data['a'] = 1  # Updates if 'a' exists, else adds 'a'
# OR
data.update({'a': 1})
# OR
data.update(dict(a=1))
# OR
data.update(a=1)

Inserting/Updating multiple values

data.update({'c':3,'d':4})  # Updates 'c' and adds 'd'

Python 3.9+:

The update operator |= now works for dictionaries:

data |= {'c':3,'d':4}

Creating a merged dictionary without modifying originals

data3 = {}
data3.update(data)  # Modifies data3, not data
data3.update(data2)  # Modifies data3, not data2

Python 3.5+:

This uses a new feature called dictionary unpacking.

data = {**data1, **data2, **data3}

Python 3.9+:

The merge operator | now works for dictionaries:

data = data1 | {'c':3,'d':4}

Deleting items in dictionary

del data[key]  # Removes specific element in a dictionary
data.pop(key)  # Removes the key & returns the value
data.clear()  # Clears entire dictionary

Check if a key is already in dictionary

key in data

Iterate through pairs in a dictionary

for key in data: # Iterates just through the keys, ignoring the values
for key, value in d.items(): # Iterates through the pairs
for key in d.keys(): # Iterates just through key, ignoring the values
for value in d.values(): # Iterates just through value, ignoring the keys

Create a dictionary from two lists

data = dict(zip(list_with_keys, list_with_values))
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-concatenate-dictionary-string-values
Python - Concatenate Dictionary string values - GeeksforGeeks
July 12, 2025 - dict.update() allows us to concatenate string values directly within the original dictionary. Instead of creating a new dictionary, this method modifies the existing one by appending the values from the second dictionary.
🌐
Studytonight
studytonight.com › python › dictionaries-in-python
Python Dictionaries - Create, Append, Delete and Update | Studytonight
Dictionaries in Python. In this tutorial you will learn about Dictionaries in python. It covers how to create a dictionary, how to access its elements, delete elements, append elements to dictionary, update a dictionary etc.