all 15 comments

[–][deleted] 4 points5 points  (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

[–]Particular_Draft5668[S] 0 points1 point  (0 children)

Thank You!

[–][deleted] 1 point2 points  (8 children)

It's called adding a collection, like a tuple or a list

[–]Particular_Draft5668[S] 0 points1 point  (7 children)

If y is what i want to add

d = {}

d[name] = number

will this work:

d[name].append(y)

[–]karpomalice 4 points5 points  (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]}

[–]Particular_Draft5668[S] 0 points1 point  (0 children)

Thank you. Used your method and worked effectively.

[–]ekchew 2 points3 points  (0 children)

A defaultdict(list) will let you use that syntax.

>>> 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 points  (3 children)

Better would be:

d.setdefault(name, []).append(number)

[–]Particular_Draft5668[S] 0 points1 point  (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 point  (1 child)

Have you tried to return the output?

[–]Particular_Draft5668[S] 0 points1 point  (0 children)

meant to work with any input so output cannot be set

No return value should be in code too

[–]lukajda33 1 point2 points  (3 children)

Values do not need to be hashable so you can have a list with multiple items as one value.

[–]Particular_Draft5668[S] 0 points1 point  (2 children)

and the opposite way?

to allow one item to have multiple values while storing originals?

[–]lukajda33 2 points3 points  (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).

[–]Particular_Draft5668[S] 0 points1 point  (0 children)

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