Created
September 13, 2012 01:50
-
-
Save johnschimmel/3711344 to your computer and use it in GitHub Desktop.
ITP DWD Week 2 - Dictionary Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# create a dictionary | |
# mydict = { key1:value1, key2:value2 } | |
mission_dates = { | |
'Gemini 3' : '23 March 1965', | |
'Gemini 10' : '18-21 July 1966', | |
'Apollo 10' : '18-26 May 1969', | |
'Apollo 16' : '16-27 April 1972 ', | |
'STS-1' : '12-14 April 1981', | |
'STS-9' : '28 November - 6 December 1983' | |
} | |
# print the date of Apollo 16 mission | |
print "\n***** access dictionary with [key] *****" | |
print mission_dates['Apollo 16'] | |
# Suggested way to retrieve key from dictionary. .get() provides a default value | |
print "\n***** .get() *****" | |
print mission_dates.get('Apollo 16') | |
# retrieve a list of all dictionary keys | |
print "\n***** .keys() *****" | |
print mission_dates.keys() | |
# retrieve a list of all dictionary values | |
print "\n***** .values() *****" | |
print mission_dates.values() | |
# print out the mission_dates with .items() --- will be a list of tuples | |
print "\n***** .items() *****" | |
print mission_dates.items() | |
# remove a item from dictionary by key | |
del mission_dates['STS-9'] | |
# loop and print dictionary keys and values | |
print "\n***** Loop and Print *****" | |
for name, date in mission_dates.items(): | |
print name, "on", date |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment