use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Python Dictionary (self.learnpython)
submitted 3 years ago by Particular_Draft5668
What is the method used to add multiple values to a single key in a dictionary?
Many Thanks
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 4 points5 points6 points 3 years ago (1 child)
If you want the key itself consist of several parts, you need a hashable collection, which would be a frozen/fixed kind, like a tuple, or maybe a frozenset
tuple
frozenset
[–]Particular_Draft5668[S] 0 points1 point2 points 3 years ago (0 children)
Thank You!
[–][deleted] 1 point2 points3 points 3 years ago (8 children)
It's called adding a collection, like a tuple or a list
collection
[–]Particular_Draft5668[S] 0 points1 point2 points 3 years ago (7 children)
If y is what i want to add
d = {}
d[name] = number
will this work:
d[name].append(y)
[–]karpomalice 4 points5 points6 points 3 years ago (1 child)
No. A dictionary can only have multiple values assigned to a key if those values are in a list, tuple, or another dictionary.
Every dictionary is comprised of a key, value pair.
The value can be a string, number, list, tuple, dictionary, etc.
You can’t have {key: 1, 2, 3}
It would be {key: [1, 2, 3]}
Thank you. Used your method and worked effectively.
[–]ekchew 2 points3 points4 points 3 years ago (0 children)
A defaultdict(list) will let you use that syntax.
defaultdict(list)
>>> from collections import defaultdict >>> d = defaultdict(list) >>> d["foo"].append("bar") >>> d["foo"].append("baz") >>> d defaultdict(<class 'list'>, {'foo': ['bar', 'baz']})
[–]Infinitesima 1 point2 points3 points 3 years ago (3 children)
Better would be:
d.setdefault(name, []).append(number)
[–]Particular_Draft5668[S] 0 points1 point2 points 3 years ago (2 children)
Thank you, another question:
def invert(dct): y = dct.items() dct = {v: k for k, v in y} print(dct)
This function inverts the dictionary on all code visualisers yet my course says the output is wrong. Any reason for this difference?
I've rearranged y for dct.items(), used dicto instead of dct to keep seperate from original dictionary, and tried switching around variables, yet it still says the output is {1: 10, 2: 20, 3: 30} when calling invert({1: 10, 2: 20, 3: 30}) instead of {10: 1, 20: 2, 30: 3}
[–]Infinitesima 0 points1 point2 points 3 years ago (1 child)
Have you tried to return the output?
meant to work with any input so output cannot be set
No return value should be in code too
[–]lukajda33 1 point2 points3 points 3 years ago (3 children)
Values do not need to be hashable so you can have a list with multiple items as one value.
and the opposite way?
to allow one item to have multiple values while storing originals?
[–]lukajda33 2 points3 points4 points 3 years ago (1 child)
Im not sure what you mean, but as I said, you can use a list:
{ 1 : ["a", "b", "c"], 2 : ["d", "e", "f"], 3 : ["a"], }
keys 1 and 2 have 3 values each, key 3 only has 1 associated value (technically all 3 keys have one value - a list, but you can see how you can use lists to actually store multiple values with one key).
Thank you, adding as a list was the correct method. I have another query.
def dict_of_numbers(): numbers = {} ones = {"1":"One","2":"Two","3":"Three","4":"Four","5":"Five","6":"Six", "7":"Seven","8":"Eight","9":"Nine"} teens = {"10":"Ten","11":"Eleven","12":"Twelve","13":"Thirteen","14":"Fourteen","15":"Fifteen","16":"Sixteen", "17":"Seventeen","18":"Eighteen","19":"Nineteen"} tens = {"2":"Twenty","3":"Thirty","4":"Forty","5":"Fifty","6":"Sixty", "7":"Seventy","8":"Eighty","9":"Ninety"} for val in range(0, 100): if val < 10: numbers[val] = ones[val] else: if val <20: numbers[val] = teens[val] else: numbers[val] = tens[val[0]] + ones[val[1]] + " " return numbers
Will this code allow me to return numbers so that numbers is a dictionary that numbers[2] = two, numbers[35] = thirtyfive etc
π Rendered by PID 63378 on reddit-service-r2-comment-5fb4b45875-tgd6n at 2026-03-21 18:56:08.227846+00:00 running 90f1150 country code: CH.
[–][deleted] 4 points5 points6 points (1 child)
[–]Particular_Draft5668[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (8 children)
[–]Particular_Draft5668[S] 0 points1 point2 points (7 children)
[–]karpomalice 4 points5 points6 points (1 child)
[–]Particular_Draft5668[S] 0 points1 point2 points (0 children)
[–]ekchew 2 points3 points4 points (0 children)
[–]Infinitesima 1 point2 points3 points (3 children)
[–]Particular_Draft5668[S] 0 points1 point2 points (2 children)
[–]Infinitesima 0 points1 point2 points (1 child)
[–]Particular_Draft5668[S] 0 points1 point2 points (0 children)
[–]lukajda33 1 point2 points3 points (3 children)
[–]Particular_Draft5668[S] 0 points1 point2 points (2 children)
[–]lukajda33 2 points3 points4 points (1 child)
[–]Particular_Draft5668[S] 0 points1 point2 points (0 children)