Python Tip: Create a test dictionary quickly

Sometimes you just want to play around with dictionaries in Python. Alright, probably not very often. But when you do, you might just want any old dictionary, and quickly if possible. Typing lots of quotes and commas and curly braces isn’t very suitable to this. So here is a fun way to make a dictionary, just add a sentence without punctuation (any set of space separated words will do, but it might as well be interesting):

test_dict = dict(enumerate('Beautiful is better than ugly'.split()))
test_dict
{0: 'Beautiful', 1: 'is', 2: 'better', 3: 'than', 4: 'ugly'}

Can’t get much easier than that.

Share and Enjoy:
  • email
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Netvibes

Post to Twitter Post to Delicious Post to Digg Post to Reddit

Possibly Related (no promises):

  1. A better way to search for methods of Python objects

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>