11 - Dictionary

 1. An empty dictionary without any items is written with just two curly braces, like this: {}. mydictionary={}
Note: The following presentation on dictionaries can be found on (assume we are referring to the Python language for this test)



 2. The following shows the creation of a simple dictionary. What is the output when run?
#creating a dictionary of key value pairs that stores the names of the upcoming iphones and their release date

#NOT SORTED  - note they are not necessarily printed/displayed in the order you create them! 
future_release = {
		"iphone10" : 2017,
		"iphone 11" : 2018,
		"iphone 12" : 2019,
		"iphone 13" : 2020,
		"iphone 14" : 2021,
		"iphone 15" : 2022


  A list of all the years (e.g. 2017, 2018 etc)

  A list of all the keys (e.g. 01, 02, 03, etc)

  A list of all the phones and the years together.

  A list of the phone versions (e.g. Iphone10, Iphone11 etc)

 3. In this Python code that has created a specific dictionary, what is happening on line 7?
def main():
    #create a dictionary to store teachers and the subject they teach
    teacher_details={"Name": "Mr Moose","Subject": "Philosophy","Hobby":"Chess","School House":"Red House"}

  We have added another key to the dictionary

  We are adding another key and associated value

  We have added another index number with a value to the dictionary

  We have added another value to the dictionary

 4. In the following code the keys are:
d = {"Ruth":30, "Jonathan":35}

  printing out all the values (e.g. Mr Moose, Philosophy)

  printing out all the keys and the index numbers

  printing out all the keys and values

  printing out all the keys (e.g. Name, Subject)

 5. For the following code, what would the output be?
d = {"Ruth":40, "Jonathan":45}
"Ruth" in d



 6. What is the output of the following code?
d = {"john":40, "peter":45}





 7. In Python: Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we use a) d.delete(“john”:40)

  del john from d

  del d(“john”:40)

  del d[“john”].


 8. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which command do we use?



  size of d()


 9. Suppose d = {“john”:40, “peter”:45}, on trying to retrieve a value using the expression d[“susan”], there is a keyError exception.



 10. What is the output of the following code that is making use of the dictionary structure?
count[(1,2,4)] = 5
count[(4,2,1)] = 7
count[(1,2)] = 6
count[(4,2,1)] = 2
tot = 0
for i in count:





 11. In the following example, what is the output?





 12. What is the result of this code?
>>> a={'B':5,'A':9,'C':7}
>>> sorted(a)





 13. What is the result of the following code snippet?
>>> a={i: i*i for i in range(6)}
>>> a  (note: dictionary comprehension is used in this example)

  None of these answers are valid

   {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

  {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6:36}

  {0: 0, 1: 1, 4: 4, 9: 9, 16: 16, 25: 25}

 14. Function all() returns True if all keys of the dictionary are true or if the dictionary is empty.
>>> b={}
>>> all(b)



 15. This code snippet has the result of inverting the key-value pairs in the dictionary
>>> a={"a":1,"b":2,"c":3}
>>> b=dict(zip(a.values(),a.keys()))
>>> b