Declaring a multi dimensional dictionary in python

A multi-dimensional dictionary is simply a dictionary where the values are themselves also dictionaries, creating a nested structure: You’d have to detect that you already created new_dic[1] each time, though, to not accidentally wipe that nested object for additional keys under new_dic[1]. You can simplify creating nested dictionaries using various techniques; using dict.setdefault() for example: dict.setdefault() will only set a key … Read more

JSON object must be str, bytes or bytearray, not dict

json.loads take a string as input and returns a dictionary as output. json.dumps take a dictionary as input and returns a string as output. With json.loads({“(‘Hello’,)”: 6, “(‘Hi’,)”: 5}), You are calling json.loads with a dictionary as input. You can fix it as follows (though I’m not quite sure what’s the point of that):

Python AttributeError: ‘dict’ object has no attribute ‘append’

Like the error message suggests, dictionaries in Python do not provide an append operation. You can instead just assign new values to their respective keys in a dictionary. If you’re wanting to append values as they’re entered you could instead use a list. Your line user[‘areas’].append[temp] looks like it is attempting to access a dictionary at the … Read more

AttributeError: ‘str’ object has no attribute ‘items’

You are passing in a string; headers can’t ever be a JSON encoded string, it is always a Python dictionary. The print results are deceptive; JSON encoded objects look a lot like Python dictionary representations but they are far from the same thing. The requests API clearly states that headers must be a dictionary: headers – (optional) Dictionary of HTTP Headers to send with the Request. JSON data is something you’d … Read more

Python Add to dictionary loop

This function is supposed to add a name and number to the dictionary ‘phoneBook’ when I run the loop, but for some reason I can’t get it to work. Any ideas on why not? Thanks a lot!

type any? has no subscript members

When you subscript profile with “Addresses”, you’re getting an Any instance back. Your choice to use Any to fit various types within the same array has caused type erasure to occur. You’ll need to cast the result back to its real type, [[String: Any]] so that it knows that the Any instance represents an Array. Then you’ll be able to subscript it: This is very … Read more