First page Back Continue Last page Overview Graphics

Simple Definition of Dictionaries

The definition of Dictionaries with Strings as keywords is “stuffed” with quotes:

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

>>> def makedict(**dictargs):

... return dictargs

...

>>>

This can be more easily achieved by using a function with “**”-argument:

>>> colours = makedict(red=1, green=2, blued=3)

>>> colours

{'green': 2, 'blued': 3, 'red': 1}

>>>

This is not necessary, because we can use “dict”:

>>> colours = dict(red=1, green=2, blued=3)

>>> colours

{'red': 1, 'blued': 3, 'green': 2}

>>>