But two important properties 1 no duplicate keys

Info icon This preview shows pages 22–25. Sign up to view the full content.

But two important properties 1. No duplicate keys allowed >>> ict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'} >>> print "dict['Name']: ", dict['Name'] dict['Name']: Manni >>> 24 CSCI2040 INTRODUCTION TO PYTHON
Image of page 22

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Dictionaries 2. Keys must be immutable i.e. lists not allowed >>> dict = {['Name']: 'Zara', 'Age': 7} Traceback (most recent call last): dict = {['Name']: 'Zara', 'Age': 7}; TypeError: unhashable type: 'list' >>> 25 CSCI2040 INTRODUCTION TO PYTHON
Image of page 23
Dictionary operations Operation Description len(d) number of elements in d d[k] item in d with key k d[k]=v set item in d with key k to v d.clear() remove all items from dictionary d d.copy make a shallow copy of d d.has_key(k) return 1 if d has key k, 0 otherwise d.items() return a list of (key,value) pair d.keys() return a list of keys in d d.values return a list of values in d d.get(k) same as d[k] d.get(k,v) return d[k] if k is valid, otherwise return v 26 Return list in Python 2.7 or earlier, but return view in 3.X or later CSCI2040 INTRODUCTION TO PYTHON
Image of page 24

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Views Instead Of Lists Dict methods return “views” instead of lists in 3.X views like a window on the keys and values (or items) of a dictionary 27 >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, 'spam': 500} >>> keys = dishes.keys() >>> values = dishes.values() >>> # view objects are dynamic and reflect dict changes >>> del dishes['eggs'] >>> keys # No eggs anymore! dict_keys(['sausage', 'bacon', 'spam']) CSCI2040 INTRODUCTION TO PYTHON
Image of page 25
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • Ruby

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern