I am learning python, and studying the dictionary concept. Trying to make a new dictionary with key and values but not able to understand why is it giving error...
adict = {}
{
“a” : “apple”
“b” : “balloon”
}
print(adict)
I am learning python, and studying the dictionary concept. Trying to make a new dictionary with key and values but not able to understand why is it giving error...
adict = {}
{
“a” : “apple”
“b” : “balloon”
}
print(adict)
Here is how you would correctly initialize a dictionary with your given keys.
adict = {'a': 'apple', 'b': 'balloon'}
Here is how you would initialize an empty dictionary.
adict = {}
The code you have just produces an invalid syntax error. It's unclear from your post which one you want. Also, good to note that you can also use, double quotes "
or single quotes '
. As one of the comments pointed out, your non-ascii quotes could also cause an error.
adict = {"a": "apple", "b": "balloon"}
Firstly, some syntax errors, try:
adict = {
"a" : "apple",
"b": "balloon"
}
print(adict)
Note the commas, and also the single pair of curly braces.
Secondly, where did you get those quotes? Did you copy and paste them from some place like a website or pdf? They are non-ascii so python won't understand them. Try typing them yourself. Thanks to chepner for pointing that out in the comments.
The following code would work.
adict = {}
adict["a"] = "apple"
adict["b"] = "baloon"
print(adict)
In the first line, you created an empty dictionary and assigned it to “adict”.
You need to separate each key-value pair with commas and make sure that your keys do not have quotation Mars. Only the values have quotation marks (if they are strings).
adict = {
“a”: “apple”,
“b”: “balloon”
}