The following code creates a simple in-memory Python movie database.
table = {'1975': 'Database', # Key: Value '1979': 'Json', '1983': 'The Web'} year = '1983' # w ww. java 2 s. c o m movie = table[year] # dictionary[Key] => Value print( movie ) for year in table: # Same as: for year in table.keys() print(year + '\t' + table[year])
The last command uses a for loop.
table = {'Database': '1975', # Key=>Value (title=>year) 'Json': '1979', 'The Web': '1983'} print( table['Database'] ) print( list(table.items()) ) # Value=>Key (year=>title) print( [title for (title, year) in table.items() if year == '1975'] )
The last command here is in part a preview for the comprehension syntax.