Other answers already adressed why this fails, here is a convenient solution that sets a default for if the key is not already present, such that your appending does not fail. The way I read it, you want a dictionary with lists of other dictionaries as values. Imagining a situation such as

Copysomedict = {}
somevar = 0
somevar_name = str(somevar)

key1 = "oh"
value1 = 1

You can do

Copysomedict.setdefault(somevar_name,[]).append({key1,value1})

This will evaluate to

{'0': [{'oh', 1}]}

In other words, change lines of this sort

Copysomedict[some_variables_name] += [{ 'somekey': somevalue }]

Into:

Copysomedict.setdefault(some_variables_name,[]).append({'somekey':somevalue})

I hope this answers your question.

Answer from Banana on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › adding key: value to empty dictionary via for loop
r/learnpython on Reddit: Adding Key: Value to Empty Dictionary via For Loop
December 3, 2022 -

Over the years I've collected a lot of quotes and I am trying to write a script to parse them out into a dictionary so I can use them in another script.

The quotes are always in this format:

> [!quote] Quote by [[Robert Greene]]
> Cultivate a fearless approach to life, attack everything with boldness and energy.

So I've created this script to get them and add them to a dictionary:

import re

quote_list = {}

with open('Quotes.md', 'r', encoding='UTF8') as file:
    for line in file:
        if '[!quote]' in line:
            author = re.findall(r'\[\[(.*?)]]', line) # Find [[author]]
            author = ' '.join(author) # remove []
            quote_list['author'] = author # add to dictionary
        elif '>' in line and not '[!quote]' in line:
            quote = line.strip('> ').strip('\n') # strip off > and \n
            quote_list['quote'] = quote

print(quote_list)

However, the final result is only 1 quote (the last quote in the document), so it appears to be overwriting the entry each time. I'm at a loss for what I'm doing wrong so any advice is much appreciated.

As I typed this out I realized that I don't think this is going to keep the authors + quotes together like I had hoped.

Example:

{'author': 'Frank A. Clark', 'quote': "We find comfort among those who agree with us - growth among those who don't"}

Update: This is the final solution I landed on. Reading line by line, if the line meets my criteria then I do what I need to for the author and then use next(file) to skip to the next line and pull the quote, finally adding it into a list as a separate dictionary.

https://pastebin.com/nuiT8Nry

Discussions

Appending elements to an empty dictionary of lists in Python - Stack Overflow
In other words, I have an empty dictionary, and as I read in values one by one from a file, I would like to append them one by one to a given dictionary key (which may or may not exist already). The challenge is that I can't create the list at once. I can only append values one by one, but I'm not sure how I can tell Python that I want a dictionary of lists when I add ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to add new keys to an empty dictionary as it goes/reads through an input list? - Stack Overflow
I would like to add new keys to an empty dictionary as it goes/reads through my list. I got this to work when I used a list containing only numbers. However, if I try this with a list which contains More on stackoverflow.com
🌐 stackoverflow.com
Adding Key: Value to Empty Dictionary via For Loop
dictionary keys are "unique" - so you are overwriting the previous entry each time. >>> quotes = {} >>> quotes['author'] = 'quote1' >>> quotes {'author': 'quote1'} >>> quotes['author'] = 'quote2' >>> quotes {'author': 'quote2'} Perhaps you want a list of dicts? [ { 'author': 'quote1' }, { 'author': 'quote2' }, ... ] More on reddit.com
🌐 r/learnpython
6
1
December 3, 2022
Why is it common in python to make an empty list or dict first?
In your case it’s completely useless however many times we run a loop and append to a list or add to a variable counter or many such things. This has to exist before we do so. I mean I thought a lot of list comprehension was supposed to help mitigate this very thing lol. for element in some_list: my_list.append(element*2) Will fail. More on reddit.com
🌐 r/Python
157
165
September 19, 2023
🌐
Esri Community
community.esri.com › t5 › python-questions › appending-data-to-an-empty-dictionary › td-p › 567277
Solved: Appending data to an empty dictionary - Esri Community
December 12, 2021 - I want to use the NAME field's values to be the value in the dictionary and a numeric value starting at 0 as the key if that is possible? I am unfortunately still using ArcGIS for Desktop and thus do not have access to python 3.6. ... I did. While it does create a dictionary and populates it with a value, the NAME field value becomes the key and the value portion is empty.
🌐
GeeksforGeeks
geeksforgeeks.org › python › initialize-an-empty-dictionary-in-python
Initialize an Empty Dictionary in Python - GeeksforGeeks
July 15, 2025 - To initialize an empty dictionary in Python, we need to create a data structure that allows us to store key-value pairs.
🌐
CodeRivers
coderivers.org › blog › python-empty-dictionary-and-appending
Python Empty Dictionary and Appending: A Comprehensive Guide - CodeRivers
February 22, 2026 - Here, new_dict is an empty dictionary initially. The update() method takes another dictionary (new_data in this case) and adds all its key-value pairs to new_dict.
🌐
TutorialsPoint
tutorialspoint.com › How-to-create-an-empty-dictionary-in-Python
How to create an empty dictionary in Python?
May 15, 2023 - Following is the syntax for creating the empty dictionary. ... dict1 = dict() print("Contents of the dictionary: ",dict1) dict1['colors'] = ["Blue","Green","Red"] print("Contents after inserting values: ",dict1) Contents of the dictionary: {} Contents after inserting values: {'colors': ['Blue', 'Green', 'Red']} ... dict1 = dict() print("Contents of the dictionary: ",dict1) dict1['Language'] = ["Python","Java","C"] dict1['Year'] = ['2000','2020','2001'] print("Contents after adding key-value pairs: ",dict1)
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to add items to a python dictionary
How to Add Items to a Python Dictionary - Spark By {Examples}
May 31, 2024 - Python provides various ways to add items or elements to a dictionary. Dictionaries are mutable, so we can add, delete, and update the dictionaries. In
🌐
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
🌐
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.
🌐
iO Flood
ioflood.com › blog › python-add-to-dictionary
Learn Python: How To Add to Dictionary (With Examples)
January 30, 2024 - The update() method is a powerful tool for adding multiple items to a dictionary or updating an existing item. However, it’s important to remember that if a key in the dictionary you’re updating matches a key in the dictionary you’re adding, ...
🌐
PyTutorial
pytutorial.com › python-empty-dictionary-guide-examples
PyTutorial | Python Empty Dictionary Guide & Examples
January 27, 2026 - # Create an empty dictionary using dict() another_empty_dict = dict() print(another_empty_dict) print(type(another_empty_dict)) ... Both methods produce the same result. The curly braces method is slightly faster and more idiomatic.
🌐
Python Forum
python-forum.io › thread-30725.html
create empty sets for keys in a dictionary and add values to the set
November 3, 2020 - so what I wanna do is this but something cleaner(short): dic = {} if key0 not in dic.keys(): dic[key0] = set() dic[key0].add(2) else: dic[key0].add(2)What I really wanted was to run a loop and add values (to the set) for the corresponding keys if ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › adding-items-to-a-dictionary-in-a-loop-in-python
Adding Items to a Dictionary in a Loop in Python - GeeksforGeeks
July 23, 2025 - res.update({i: j}) adds each key-value pair to the res dictionary. ... This method involves directly assigning values to the dictionary keys by iterating through both the keys and values. We can use a loop to access each key and then use the index to retrieve the corresponding value from a separate list. ... a = ['Name', 'Website', 'Topic', 'Founded'] b = ['GeeksforGeeks', 'https://www.geeksforgeeks.org/', 'Programming', 2009] res = {} # initializes an empty dictionary for i in range(len(a)): res[a[i]] = b[i] print(res)
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to create python empty dictionary?
How to Create Python Empty Dictionary? - Spark By {Examples}
May 31, 2024 - You can use the zip() and len() methods to create an empty dictionary with keys and no values. Python zip() is a built-in function that is used to zip or combine the elements from two or more iterable objects like lists, tuples, dictionaries, etc.
🌐
FavTutor
favtutor.com › blogs › dictionary-append-python
Dictionary Append in Python | 3 Methods (with code)
September 11, 2023 - Learn various methods for how to append values in a dictionary in Python including update method or dictionary comprehension.
🌐
FavTutor
favtutor.com › blogs › empty-dictionary-python
Create Empty Dictionary in Python (5 Easy Ways) | FavTutor
April 13, 2022 - If no arguments are provided, an empty dictionary is created. ... In Python, we can use the zip() and len() methods to create an empty dictionary with keys.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-add-new-keys-to-a-dictionary
Add new keys to a dictionary in Python - GeeksforGeeks
July 11, 2025 - ... d = {"a": 1, "b": 2} # Adding ... exists then its value is updated. We can use | operator to create a new dictionary by merging existing dictionaries or adding new keys and values....
🌐
HCL GUVI
studytonight.com › python › dictionaries-in-python
HCL GUVI | Learn to code in your native language
HCL GUVI's Data Science Program was just fantastic. It covered statistics, machine learning, data visualization, and Python within its curriculum.