1 / 9

Lecture 20

Lecture 20. Dictionaries. What is a dictionary?. In a list we can identify an retrieve an item by its position or index. A dictionary uses key, value pairs. This association between a key and its value is called a mapping . Here is an example of a dictionary:

jana-bryant
Download Presentation

Lecture 20

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 20 Dictionaries

  2. What is a dictionary? • In a list we can identify an retrieve an item by its position or index. • A dictionary uses key, valuepairs. This association between a key and its value is called a mapping. • Here is an example of a dictionary: • phoneList = {“Mary”: “276-5402”, “Jim”: “276-4106”, “Tom”: “378-9787”} • keys can be strings or integers • Values can be anything

  3. Accessing a dictionary • phoneList = {“Mary”: “276-5402”, “Jim”: “276-4106”, “Tom”: “378-9787”} • >>print phoneList[“Mary”] • >> 276-5402 • Dictionaries are mutable – I can change the contents • >> phoneList[“Mary”] = “236-3435” • >> print phoneList[“Mary”] • >> 236-3435

  4. Operations on a dictionary • You can print it, but it may not be in the same order as when you created it • >>> phoneList = {'Mary': "236-3435", "Jim":'276-4106', 'Tom': '378-9787'} • >>> print phoneList • {'Jim': '276-4106', 'Mary': '236-3435', 'Tom': '378-9787'}

  5. To add to a Dictionary • Simply assign to a new or old key • >>> phoneList["Drew"] = "289-5667“ • >>> print phoneList • {'Jim': '276-4106', 'Mary': '236-3435', 'Drew': '289-5667', 'Tom': '378-9787'} • >>>

  6. Dictionary Methods • Getting a list of all the keys or values in the dictionary • >>> phoneList.keys() • ['Jim', 'Mary', 'Drew', 'Tom'] • >>> phoneList.values() • ['276-4106', '236-3435', '289-5667', '378-9787']

  7. Methods continued • Checking if a key is in the dictionary • >>> phoneList.has_key("Mary") • True • >>> 'Mary' in phoneList • True • >>> 'John' in phoneList • False

  8. More methods • Getting all the pairs in the dictionary • >>> phoneList.items() • [('Jim', '276-4106'), ('Mary', '236-3435'), ('Drew', '289-5667'), ('Tom', '378-9787')] • Another way to get a value if you don’t know it is in the dictionary • >>> phoneList.get("Mary", "No phone") • '236-3435' • >>> phoneList.get("John", "No phone") • 'No phone'

  9. Removing from a dictionary • >>> del phoneList["Mary"] • >>> print phoneList • {'Jim': '276-4106', 'Drew': '289-5667', 'Tom': '378-9787'} • >>> phoneList.clear() • phoneList still exists but you have removed all the pairs.

More Related