Test for membership

Documentation

  1. Class set in the Python Standard Library
  2. Sets in the Python Tutorial
  3. Set types in the Python Language Reference

Test for membership

I’d never want to belong to any club that would have me for a member.
     —Groucho Marx

The following program would still work if legalStates were a list or a tuple, but the membership test operator in can search a set faster.

"""
Test for membership in a set.
"""

import sys

legalStates = {   #a set of strings
    "California",
    "Colorado",
    "Maine",
    "Massachusetts",
    "Michigan",
    "Oregon",
    "Vermont",
    "Washington"
}

try:
    state = input("What state are you interested in? ")
except BaseException:
    sys.exit(1)

if state in legalStates:   #in is a membership test operator
    print(f"Marijuana is legal in {state}.")
else:
    print(f"Marijuana is partially or fully illegal in {state}.")

sys.exit(0)
What state are you interested in? Massachusetts
Marijuana is legal in Massachusetts.
What state are you interested in? Massachusets
Marijuana is partially or fully illegal in Massachusets.

A dictionary

"""
Look up a state in a dictionary and find the corresponding bool.
"""

import sys

states = {
    "Alabama":        False,
    "Alaska":         False,
    "Arizona":        False,
    "Arkansas":       False,
    "California":     True,
    "Colorado":       True,
    "Connecticut":    False,
    "Delaware":       False,
    "Florida":        False,
    "Georgia":        False,
    "Hawaii":         False,
    "Idaho":          False,
    "Illinois":       False,
    "Indiana":        False,
    "Iowa":           False,
    "Kansas":         False,
    "Kentucky":       False,
    "Louisiana":      False,
    "Maine":          True,
    "Maryland":       False,
    "Massachusetts":  True,
    "Michigan":       False,
    "Minnesota":      False,
    "Mississippi":    False,
    "Missouri":       False,
    "Montana":        False,
    "Nebraska":       False,
    "Nevada":         False,
    "New Hampshire":  False,
    "New Jersey":     False,
    "New Mexico":     False,
    "New York":       False,
    "North Carolina": False,
    "North Dakota":   False,
    "Ohio":           False,
    "Oklahoma":       False,
    "Oregon":         True,
    "Pennsylvania":   False,
    "Rhode Island":   False,
    "South Carolina": False,
    "South Dakota":   False,
    "Tennessee":      False,
    "Texas":          False,
    "Utah":           False,
    "Vermont":        True,
    "Virginia":       False,
    "Washington":     True,
    "West Virginia":  False,
    "Wisconsin":      False,
    "Wyoming":        False
}

try:
    state = input("What state are you interested in? ")
except BaseException:
    sys.exit(1)

try:
    isLegal = states[state]
except KeyError:
    print(f'Sorry, "{state}" is not a state.')
    sys.exit(1)

if isLegal:
    print(f"Marijuana is legal in {state}.")
else:
    print(f"Marijuana is partially or fully illegal in {state}.")

sys.exit(0)
What state are you interested in? Massachusetts
Marijuana is legal in Massachusetts.
What state are you interested in? Massachusets
Sorry, "Massachusets" is not a state.

Things to try

  1. Do the work in pandas.
    """
    Look up an index in a pandas Series.
    """
    
    import sys
    import pandas as pd
    
    states = {
        "Alabama":        False,
        "Alaska":         False,
        "Arizona":        False,
        "Arkansas":       False,
        "California":     True,
        "Colorado":       True,
        "Connecticut":    False,
        "Delaware":       False,
        "Florida":        False,
        "Georgia":        False,
        "Hawaii":         False,
        "Idaho":          False,
        "Illinois":       False,
        "Indiana":        False,
        "Iowa":           False,
        "Kansas":         False,
        "Kentucky":       False,
        "Louisiana":      False,
        "Maine":          True,
        "Maryland":       False,
        "Massachusetts":  True,
        "Michigan":       False,
        "Minnesota":      False,
        "Mississippi":    False,
        "Missouri":       False,
        "Montana":        False,
        "Nebraska":       False,
        "Nevada":         False,
        "New Hampshire":  False,
        "New Jersey":     False,
        "New Mexico":     False,
        "New York":       False,
        "North Carolina": False,
        "North Dakota":   False,
        "Ohio":           False,
        "Oklahoma":       False,
        "Oregon":         True,
        "Pennsylvania":   False,
        "Rhode Island":   False,
        "South Carolina": False,
        "South Dakota":   False,
        "Tennessee":      False,
        "Texas":          False,
        "Utah":           False,
        "Vermont":        True,
        "Virginia":       False,
        "Washington":     True,
        "West Virginia":  False,
        "Wisconsin":      False,
        "Wyoming":        False
    }
    
    series = pd.Series(states)
    #s = series[:3].to_string()
    #print(s)
    #print(f"The first index is {series.index[0]}.")
    #print(f"The first value is {series.array[0]}.")
    
    try:
        state = input("What state are you interested in? ")
    except BaseException:
        sys.exit(1)
    
    try:
        isLegal = series[state]
    except KeyError:
        print(f'Sorry, "{state}" is not a state.')
        sys.exit(1)
    
    if isLegal:
        print(f"Marijuana is legal in {state}.")
    else:
        print(f"Marijuana is partially or fully illegal in {state}.")
    
    sys.exit(0)