I l@ve RuBoard Previous Section Next Section

1.3 Constructing a Dictionary Without Excessive Quoting

Credit: Brent Burley

1.3.1 Problem

You'd like to construct a dictionary without having to quote the keys.

1.3.2 Solution

Once you get into the swing of Python, you may find yourself constructing a lot of dictionaries. However, the standard way, also known as a dictionary display, is just a smidgeon more cluttered than you might like, due to the need to quote the keys. For example:

data = { 'red' : 1, 'green' : 2, 'blue' : 3 }

When the keys are identifiers, there's a cleaner way:

def makedict(**kwargs):
    return kwargs
data = makedict(red=1, green=2, blue=3)

You might also choose to forego some simplicity to gain more power. For example:

def dodict(*args, **kwds):
    d = {}
    for k, v in args: d[k] = v
    d.update(kwds)
    return d
tada = dodict(*data.items(  ), yellow=2, green=4)

1.3.3 Discussion

The syntax for constructing a dictionary can be slightly tedious, due to the amount of quoting required. This recipe presents a technique that avoids having to quote the keys, when they are identifiers that you already know at the time you write the code.

I've often found myself missing Perl's => operator, which is well suited to building hashes (Perl-speak for dictionaries) from a literal list:

%data = (red => 1, green => 2, blue => 3);

The => operator in Perl is equivalent to Perl's own ,, except that it implicitly quotes the word to its left.

Perl's syntax is very similar to Python's function-calling syntax for passing keyword arguments. And the fact that Python collects the keyword arguments into a dictionary turned on a light bulb in my head.

When you declare a function in Python, you may optionally conclude the list of formal arguments with *args or **kwds (if you want to use both, the one with ** must be last). If you have *args, your function can be called with any number of extra actual arguments of the positional, or plain, kind. Python collects all the extra positional arguments into a tuple and binds that tuple to the identifier args. Similarly, if you have **kwds, your function can be called with any number of extra actual arguments of the named, or keyword, kind. Python collects all the extra named arguments into a dictionary (with the names as the keys and the values as the values) and binds that dictionary to the identifier kwds. This recipe exploits the way that Python knows how to perform the latter task.

The makedict function should be very efficient, since the compiler is doing work equivalent to that done with a dictionary literal. It is admittedly idiomatic, but it can make large dictionary literals a lot cleaner and a lot less painful to type. When you need to construct dictionaries from a list of key/item pairs, possibly with explicit override of, or addition to, some specifically named key, the dodict function (although less crystal-clear and speedy) can be just as handy. In Python 2.2, the first two lines of dodict can be replaced with the more concise and faster equivalent:

d = dict(args)

1.3.4 See Also

The Library Reference section on mapping types.

    I l@ve RuBoard Previous Section Next Section