﻿ Lesson 23: Dictionaries

Fundamentals of Dictionaries

Introduction

We have been introduced to lists as series of items. To create such a list, we provided the items, one at a time in square brackets. Here is an example:

```numbers : list = [ 480272, 297314, 835026, 138049, 472555 ]

print("Number: ", numbers[0])
print("Number: ", numbers[1])
print("Number: ", numbers[2])
print("Number: ", numbers[3])
print("Number: ", numbers[4])
print("=================================")```

This would produce:

```Number:  480272
Number:  297314
Number:  835026
Number:  138049
Number:  472555
=================================
Press any key to continue . . .```

That is one type of list. Another type of list is called a dictionary. A dictionary is a type of list where each item is in two parts: a key and a value. The combination can be represented as key-value or key/value or else.

Creating a Dictionary

To create a dictionary, you create a list of items between curly brackets, { and }. The items are separated by commas. Remember that each item of a ditionary is made in two parts. The parts are separated by a colon. Therefore, each item of a dictionary is in the form key:value. Both parts can be numbers. Here is an example of a dictionary:

`salaries = { 1:44600, 2:36845, 3:82460, 4:28535, 5:102336 }`

Both parts can be strings. Here is an example:

`employees = { "Raymond":"Mueller", "Jeannette":"Sodrigo", "Robert":"Gibbs" }`

One part can be a string while the other is a number and vice-versa. Here is an example:

`staff_members = { 480272 : "Raymond Mueller", 297314 : "Jeannette Sodrigo", 835026 : "Robert Gibbs" }`

Accessing the Values of a Dictionary

Remember that each item of a dictionary is made of two parts, a key and a value. The key serves as a type of index for the value, but that index is not based on position as seen with lists. The key of an item allows you to access the corresponding value. Therefore, to access the value of an item of a dictionary, apply the square brackets to the dictionary variable. In the square brackets, type the key of the item. Here are examples:

```staff_members = { 480272 : "Raymond Mueller", 297314 : "Jeannette Sodrigo", 835026 : "Robert Gibbs" }

print("Employee #: ", staff_members[480272])
print("Employee #: ", staff_members[297314])
print("Employee #: ", staff_members[835026])
print("=================================")```

This would produce:

```Employee #:  Raymond Mueller
Employee #:  Jeannette Sodrigo
Employee #:  Robert Gibbs
=================================
Press any key to continue . . .```

Dictionaries and Functions

A Parameter as a List

You can create a function that uses a collection as argument. To do this, in the parentheses of the function, start the name of the argument with **. Here is an example:

```def getSomeNames(**values):
. . .```

In the body of the function, you can ignore or use the parameter. As seen with the other parameters so far, you can pass the parameter to the print() function. Here is an example:

```def getSomeNames(**values):
print(values)```

When calling the function, you must provide a value for the parameter. The argument of the parameter must be passed as a collection. You have various options.

Although we haven't studied collections yet, we can still find out that the minimum way to create a collection is to create a list of values and put that list between { and }. The items of the list must be separated with commas. Therefore, when calling a function that takes a **parameter-name parameter, pass the argument as a collection. Also, you must name the argument; that is, in the parentheses of calling the function, you must assign the collection to a name. Here is an example:

```def getSomeNames(**values):
print(values)

getSomeNames(names={ "Thomas", "Christine", "Michael", "Veronican" })
print("=============================================================")```

This would produce:

```{'names': {'Veronican', 'Thomas', 'Christine', 'Michael'}}
=============================================================
Press any key to continue . . .```

The Key to a Collection of Parameters

Normally, when you create a function that uses a collection as parameter, you should add another parameter that is referred to as a key (we will come back to all those issues when we study collections). The collection you provide to a function works like a dictionary, which is a collection of items that function as key-value pairs. In this case, an item can have only one key, but it can have one or more values. Based on this, when setting up the parameters of the function, pass a first argument as the key that holds one value. Pass the second parameter as a collection. Here is an example:

```def decribe(name, **selections):
. . .```

Once again, in the body of the function, you can ignore or use one or both parameters. At a minimum, you can pass those parameters to print() to display their value. Here are example:

```def decribe(name, **selections):
print("Vehicle: ", name)
print("options: ", selections)```

When calling the function, you have three primary options. You can call the function with one regular value. In this can, only the key parameter would be processed. Here is an example:

```def decribe(name, **selections):
print("Vehicle: ", name)
print("options: ", selections)

decribe("Vehicle")
print("-----------------------------------------------------------------------")```

This would produce

```Vehicle:  Vehicle
options:  {}
-----------------------------------------------------------------------
Press any key to continue . . .```

You can pass only a collection as argument. In that case, the compiler would think that the single argument is for the first parameter. Here is an example:

```def decribe(name, **selections):
print("Vehicle: ", name)
print("options: ", selections)

decribe("Vehicle");
print("-----------------------------------------------------------------------")
decribe({ "Willys Sport", "Altitude", "Freedom", "Rubicon" })
print("-----------------------------------------------------------------------")```

This would produce:

```Vehicle:  Vehicle
options:  {}
-----------------------------------------------------------------------
Vehicle:  {'Willys Sport', 'Altitude', 'Rubicon', 'Freedom'}
options:  {}
-----------------------------------------------------------------------
Press any key to continue . . .```

If you want to use both parameters, when calling the function, pass a value for the first argument (the first parameter of the function). For the second argument, specify a name for each value of the collection and assign a value of the collection the parameter in that position. Here is an example:

```def decribe(name, **selections):
print("Vehicle: ", name)
print("options: ", selections)

decribe("Vehicle");
print("-----------------------------------------------------------------------")
decribe({ "Willys Sport", "Altitude", "Freedom", "Rubicon" });
print("-----------------------------------------------------------------------")
decribe("Jeep Wrangler", base = "Sport", terrain = "Sahara", solid = "Islander")
print("=======================================================================")```

This would produce:

```Vehicle:  Vehicle
options:  {}
-----------------------------------------------------------------------
Vehicle:  {'Freedom', 'Rubicon', 'Altitude', 'Willys Sport'}
options:  {}
-----------------------------------------------------------------------
Vehicle:  Jeep Wrangler
options:  {'base': 'Sport', 'terrain': 'Sahara', 'solid': 'Islander'}
=======================================================================
Press any key to continue . . .```