Python - Using dictionary as Database

Introduction

The following code creates a simple in-memory Python movie database.

Demo

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])

Result

The last command uses a for loop.

Mapping values to keys

Demo

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'] )

Result

The last command here is in part a preview for the comprehension syntax.