Sunday, October 15, 2006

Iterating Python Dictionaries

mimeboundarymarker: Python iteritems function:

"The iteritems function returns an iterator over the key and value pairs in a Python dictionary

>>> wordz = {'school' : 'schule', 'weather' : 'wetter'}"



We have a dictionary called wordz above with two items. The usual way I used to iterate over these items was the following:

keys = wordz.keys()
for x in keys:
.... print x, "-->", wordz[x]

The above code would go over the list of keys, and print its corresponding entry in the dictionary. Thanks to the post linked above I learnt about an easier way of iterating over a dictionary using the iteritems keyword:

>>> for key, value in wordz.iteritems():
....... print key, "=", value


I've always been looking to improve the semantic experience of the web. So when I read Steve Rubels article on using technorati RSS feeds, I jumped at the chance to use them. I found the above post as a result of one of those RSS feeds.

The internet gets more enjoyable by the day.