Pages

Python: For in Dictionary

We can use 'for in' for dictionaries as list items.

dict = {'white' : 'clear soul' , 'flow' : 'contiunity' , 'name' : 'flow chart'}

for i in dict:
    print i

This will bring the keywords : white , flow, name

for i in dict:
    print dict[i]

This will bring the meanings: clear soul, contiunity, flow chart

If we want to get the value of a key in the dict we must check if it is in the dict or not. Otherwise we can get error. So we must check the availability first.

dict = {'white' : 'clear soul' , 'flow' : 'contiunity' , 'name' : 'flow chart'}
search = raw_input('What do you search?:>)

if search in dict:
    print dict[search]
else:
    print "This is not in dictionary."