How to Replace the switch Statement with a Dictionary in Python

By John Paul Mueller

Most programming languages (Python excluded), provide some sort of switch statement. A switch statement provides for elegant menu type selections. The user has a number of options but is allowed to choose only one of them. The program takes some course of action based on the user selection. Here is some representative code (it won’t execute) of a switch statement you might find in another language:

switch(n)
{
 case 0:
  print("You selected blue.");
  break;
 case 1:
  print("You selected yellow.");
  break;
 case 2:
  print("You selected green.");
  break;
}

The application normally presents a menu-type interface, obtains the number of the selection from the user, and then chooses the correct course of action from the switch statement. It’s straightforward and much neater than using a series of if statements to accomplish the same task.

Unfortunately, Python doesn’t come with a switch statement. The best you can hope to do is use an if…elif statement for the task. However, by using a dictionary, you can simulate the use of a switch statement. The following steps help you create an example that will demonstrate the required technique.

1Open a Python File window.

You see an editor in which you can type the example code.

2Type the following code into the window — pressing Enter after each line:

def PrintBlue():
 print("You chose blue!rn")
def PrintRed():
 print("You chose red!rn")
def PrintOrange():
 print("You chose orange!rn")
def PrintYellow():
 print("You chose yellow!rn")

Before the code can do anything for you, you must define the tasks. Each of these functions defines a task associated with selecting a color option onscreen. Only one of them gets called at any given time.

3Type the following code into the window — pressing Enter after each line:

ColorSelect = {
 0: PrintBlue,
 1: PrintRed,
 2: PrintOrange,
 3: PrintYellow
}

This code is the dictionary. Each key is like the case part of the switch statement. The values specify what to do. In other words, this is the switch structure. The functions that you created earlier are the action part of the switch — the part that goes between the case statement and the break clause.

4Type the following code into the window — pressing Enter after each line:

Selection = 0
while (Selection != 4):
 print("0. Blue")
 print("1. Red")
 print("2. Orange")
 print("3. Yellow")
 print("4. Quit")
 Selection = int(input("Select a color option: "))
 if (Selection >= 0) and (Selection < 4):
  ColorSelect[Selection]()

Finally, you see the user interface part of the example. The code begins by creating an input variable, Selection. It then goes into a loop until the user enters a value of 4.

During each loop, the application displays a list of options and then waits for user input. When the user does provide input, the application performs a range check on it. Any value between 0 and 3 selects one of the functions defined earlier using the dictionary as the switching mechanism.

5Choose Run→Run Module.

You see a Python Shell window open. The application displays a menu.

6Type 0 and press Enter.

The application tells you that you selected blue and then displays the menu again.

7Type 4 and press Enter.

The application ends.