words = ['hello', 'world', 'from', 'python'] for w in words: print w
- a real world sample: (create list of words used in a book chapter titles)
#'titles' contains a list of chapter titles (strings) wordDB = {} #create an empty dictionary for title in titles: #iterate over the list of titles words = string.split(title) #get list of words for word in words: #put a word into the dictionary wordDB[string.upper(word)] = 1 print wordDB.keys() #print the list of words