Python Dictionary
We also have a Python dictionary that you may use since once you master it, a whole universe of ultra-cool will be at your disposal. The dictionary is the greatest storage device ever. In Python, they are referred to as “dicts.” In other languages, they are known as “hashes.” It makes no difference whatever name I prefer to use. What counts is how they perform when measured against lists. See, a list enables you to perform the following:
>>> things = ['a', 'b', 'c', 'd'] >>> print things[1] b >>> things[1] = 'z' >>> print things[1] z >>> print things ['a', 'z', 'c', 'd'] >>>
Numbers can be used to “index” into a list, or to determine what is on a given list. By now, you ought to be aware of the fact that you may only remove items from a list using their respective numbers. You can utilize anything, not just numbers, thanks to dictionaries. Yes, a dictionary will always relate one item to another. Look at this:
>>> stuff = {'name': 'Zed', 'age': 36, 'height': 6*12+2} >>> print stuff['name'] Zed >>> print stuff['age'] 36 >>> print stuff['height'] 74 >>> stuff['city'] = "San Francisco" >>> print stuff['city'] San Francisco >>>
Python dictionary
You’ll see that we’re utilizing strings rather than just integers to specify what we want from the things dictionary. With strings, we can add new terms to the dictionary. However, it doesn’t have to be strings. Additionally, we can:
>>> stuff[1] = "Wow" >>> stuff[2] = "Neato" >>> print stuff[1] Wow >>> print stuff[2] Neato >>> print stuff {'city': 'San Francisco', 2: 'Neato', 'name': 'Zed', 1: 'Wow', 'age': 36, 'height': 74} >>>
In this code I used numbers, and then you can see there are numbers and strings as keys in the python dictionary when I print it. I could use anything—well, almost, but just pretend you can use anything for now.
Of course, a dictionary that you can only put things, so here’s how you delete things, with the del keyword:
>>> del stuff['city'] >>> del stuff[1] >>> del stuff[2] >>> stuff {'name': 'Zed', 'age': 36, 'height': 74} >>>
We’ll now perform a task that requires thorough consideration. Please enter this exercise and make an effort to comprehend what is happening. Note the procedures I use here, when I put things in a dict, when I get from them, etc.
# create a mapping of state to abbreviation states = [ 'Oregon': 'OR', 'Florida': 'FL', 'California': 'CA', 'New York': 'NY', 'Michigan': 'MI' ] # create a basic set of states and some cities in them cities = [ 'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville' ] # add some more cities cities['NY'] = 'New York' cities['OR'] = 'Portland' print out some cities print '- ' * 10 print "NY State has: ", cities['NY'] print "OR State has: ", cities['OR'] # print some states print '- ' * 10 print "Michigan's abbreviation is: ", states['Michigan'] print "Florida has: ", cities[states['Florida']] # do it by using the state then cities dict print '- ' * 10 print "Michigan has: ", cities[states['Michigan']] print "Florida has: ", cities[states['Florida']] # print every state abbreviation print '- ' * 10 for state, abbrev in states.items(): print "%s is abbreviated %s" % (state, abbrev) # print every city in state print '- ' * 10 for abbrev, city in cities.items(): print "%s has the city %s" % (abbrev, city) # now do both at the same time print '- ' * 10 for state, abbrev in states.items(): print "%s state is abbreviated %s and has city %s" % ( state, abbrev, cities[abbrev]) print '- ' * 10 # safely get an abbreviation by state that might not be there state = states.get('Texas', None) if not state: print "Sorry, no Texas." # get a city with a default value city = cities.get('TX', 'Does Not Exist') print "The city for the state 'TX' is: %s" % city
I will be adding more posts in Python Topic, so please bookmark the post for future reference too.