Skip to content

Instantly share code, notes, and snippets.

@cizixs
Created August 12, 2013 15:29
Show Gist options
  • Select an option

  • Save cizixs/6211891 to your computer and use it in GitHub Desktop.

Select an option

Save cizixs/6211891 to your computer and use it in GitHub Desktop.
This gist lists some pythonic dict manipulation from Python Cookbook.
def addItem(theIndex, word, pagenumber):
theIndex.setdefault(word, [ ]).append(pagenumber)
#the above method equals to the following
def addItem(theIndex, word, pagenumber):
if word in theIndex:
theIndex[word].append(pagenumber)
else:
theIndex[word] = [pagenumber]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment