-10

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)
Maxime B
  • 966
  • 1
  • 9
  • 30

4 Answers4

4

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"}
Skam
  • 7,298
  • 4
  • 22
  • 31
3

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.

Dillon
  • 151
  • 1
  • 4
-2

The following code would work.

adict = {}

adict["a"] = "apple"
adict["b"] = "baloon"

print(adict)
Maxime B
  • 966
  • 1
  • 9
  • 30
  • I think this is being downvoted because it isn’t the simplest way to do this. It’s easier for a small dictionary like this to write it as shown in the other answers. – Jack Moody Aug 31 '18 at 17:27
-2

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”
}
Alex Z
  • 76
  • 3