Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Python Dictionary to translate US States to Two letter codes
# United States of America Python Dictionary to translate States,
# Districts & Territories to Two-Letter codes and vice versa.
#
# https://gist.github.com/rogerallen/1583593
#
# Dedicated to the public domain. To the extent possible under law,
# Roger Allen has waived all copyright and related or neighboring
# rights to this code.
us_state_abbrev = {
'Alabama': 'AL',
'Alaska': 'AK',
'American Samoa': 'AS',
'Arizona': 'AZ',
'Arkansas': 'AR',
'California': 'CA',
'Colorado': 'CO',
'Connecticut': 'CT',
'Delaware': 'DE',
'District of Columbia': 'DC',
'Florida': 'FL',
'Georgia': 'GA',
'Guam': 'GU',
'Hawaii': 'HI',
'Idaho': 'ID',
'Illinois': 'IL',
'Indiana': 'IN',
'Iowa': 'IA',
'Kansas': 'KS',
'Kentucky': 'KY',
'Louisiana': 'LA',
'Maine': 'ME',
'Maryland': 'MD',
'Massachusetts': 'MA',
'Michigan': 'MI',
'Minnesota': 'MN',
'Mississippi': 'MS',
'Missouri': 'MO',
'Montana': 'MT',
'Nebraska': 'NE',
'Nevada': 'NV',
'New Hampshire': 'NH',
'New Jersey': 'NJ',
'New Mexico': 'NM',
'New York': 'NY',
'North Carolina': 'NC',
'North Dakota': 'ND',
'Northern Mariana Islands':'MP',
'Ohio': 'OH',
'Oklahoma': 'OK',
'Oregon': 'OR',
'Pennsylvania': 'PA',
'Puerto Rico': 'PR',
'Rhode Island': 'RI',
'South Carolina': 'SC',
'South Dakota': 'SD',
'Tennessee': 'TN',
'Texas': 'TX',
'Utah': 'UT',
'Vermont': 'VT',
'Virgin Islands': 'VI',
'Virginia': 'VA',
'Washington': 'WA',
'West Virginia': 'WV',
'Wisconsin': 'WI',
'Wyoming': 'WY'
}
# thank you to @kinghelix and @trevormarburger for this idea
abbrev_us_state = dict(map(reversed, us_state_abbrev.items()))
# Simple test examples
if __name__ == '__main__':
print("Wisconin --> WI?", us_state_abbrev['Wisconsin'] == 'WI')
print("WI --> Wisconin?", abbrev_us_state['WI'] == 'Wisconsin')
print("Number of entries (50 states, DC, 5 Territories) == 56? ", 56 == len(us_state_abbrev))
@skillachie

This comment has been minimized.

Copy link

@skillachie skillachie commented May 15, 2013

Thanks. Saved me some time

@dalguji

This comment has been minimized.

Copy link

@dalguji dalguji commented Apr 19, 2014

Awesome. Thanks!

@lwgray

This comment has been minimized.

Copy link

@lwgray lwgray commented Jul 12, 2015

Thanks save me a lot of time!

@jameserrico

This comment has been minimized.

Copy link

@jameserrico jameserrico commented Nov 11, 2015

Same here, if you're dealing with it, don't forget 'Washington, DC': 'DC',

@prithwi

This comment has been minimized.

Copy link

@prithwi prithwi commented Dec 29, 2015

Thanks!

@JiahaoShan

This comment has been minimized.

Copy link

@JiahaoShan JiahaoShan commented Apr 27, 2016

THANKS!

@kirkhunter

This comment has been minimized.

Copy link

@kirkhunter kirkhunter commented May 13, 2016

You da real mvp

@jimioke

This comment has been minimized.

Copy link

@jimioke jimioke commented Jan 5, 2017

Thanks, haha!

@drgrubman

This comment has been minimized.

Copy link

@drgrubman drgrubman commented Jan 9, 2017

Simple and incredibly helpful!

@alexroussos

This comment has been minimized.

Copy link

@alexroussos alexroussos commented Jun 2, 2017

🥇

@slowjazz

This comment has been minimized.

Copy link

@slowjazz slowjazz commented Jun 21, 2017

Thank you for this

@gassnerj

This comment has been minimized.

Copy link

@gassnerj gassnerj commented Jun 29, 2017

This is awesome! Glad I searched before making this myself! Appreciate your work on this!

@robobyn

This comment has been minimized.

Copy link

@robobyn robobyn commented Jul 5, 2017

Thanks for this! Was not looking forward to typing this out myself!!

@miaozeyu

This comment has been minimized.

Copy link

@miaozeyu miaozeyu commented Aug 10, 2017

Thanks @rogerallen!
You could also add the following:
'Northern Mariana Islands':'MP', 'Palau': 'PW', 'Puerto Rico': 'PR', 'Virgin Islands': 'VI', 'District of Columbia': 'DC'

@liyepeng

This comment has been minimized.

Copy link

@liyepeng liyepeng commented Nov 10, 2017

Thanks for your great work!

@juan1393

This comment has been minimized.

Copy link

@juan1393 juan1393 commented Dec 7, 2017

Thanks!

@ajankuv

This comment has been minimized.

Copy link

@ajankuv ajankuv commented Jan 14, 2018

thanks, saved my ass!

@kinghelix

This comment has been minimized.

Copy link

@kinghelix kinghelix commented Jan 15, 2018

Thanks for this! And here's the same data with the abbreviations as the keys (for converting codes to names - which is what I needed!)

us_state_abbrev = {
    'AL': 'Alabama',
    'AK': 'Alaska',
    'AZ': 'Arizona',
    'AR': 'Arkansas',
    'CA': 'California',
    'CO': 'Colorado',
    'CT': 'Connecticut',
    'DE': 'Delaware',
    'FL': 'Florida',
    'GA': 'Georgia',
    'HI': 'Hawaii',
    'ID': 'Idaho',
    'IL': 'Illinois',
    'IN': 'Indiana',
    'IA': 'Iowa',
    'KS': 'Kansas',
    'KY': 'Kentucky',
    'LA': 'Louisiana',
    'ME': 'Maine',
    'MD': 'Maryland',
    'MA': 'Massachusetts',
    'MI': 'Michigan',
    'MN': 'Minnesota',
    'MS': 'Mississippi',
    'MO': 'Missouri',
    'MT': 'Montana',
    'NE': 'Nebraska',
    'NV': 'Nevada',
    'NH': 'New Hampshire',
    'NJ': 'New Jersey',
    'NM': 'New Mexico',
    'NY': 'New York',
    'NC': 'North Carolina',
    'ND': 'North Dakota',
    'OH': 'Ohio',
    'OK': 'Oklahoma',
    'OR': 'Oregon',
    'PA': 'Pennsylvania',
    'RI': 'Rhode Island',
    'SC': 'South Carolina',
    'SD': 'South Dakota',
    'TN': 'Tennessee',
    'TX': 'Texas',
    'UT': 'Utah',
    'VT': 'Vermont',
    'VA': 'Virginia',
    'WA': 'Washington',
    'WV': 'West Virginia',
    'WI': 'Wisconsin',
    'WY': 'Wyoming',
}
@SilverSurfer0

This comment has been minimized.

Copy link

@SilverSurfer0 SilverSurfer0 commented Feb 20, 2018

Whoever uses this list, please beware the dict is not complete and thus introduces a bug.
(Actually took me sometime to figure it out, thus I'm a bit winded up, sorry.)

Ensure to add 'District of Columbia': 'DC' (Yes, it is very small though :))
Optionally add: 'Northern Mariana Islands':'MP', 'Palau': 'PW', 'Puerto Rico': 'PR', 'Virgin Islands': 'VI'

@rogerallen would you kindly update it. Your dictionary is one of the first on google results and could spare peoples effort for sure.

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Oct 29, 2018

Awesome, thank you!

@TomFaulkner

This comment has been minimized.

Copy link

@TomFaulkner TomFaulkner commented Jan 18, 2019

Saved me some time as well, thanks.

@trevormarburger

This comment has been minimized.

Copy link

@trevormarburger trevormarburger commented Apr 23, 2019

To extend @kinghelix post, if you need to swap K/V (or invert) a dictionary: inverted_dict = dict(map(reversed, us_state_abbrev.items()))

@Johnny512

This comment has been minimized.

Copy link

@Johnny512 Johnny512 commented Jul 8, 2019

Thank you for this.

@rogerallen

This comment has been minimized.

Copy link
Owner Author

@rogerallen rogerallen commented Jul 13, 2019

WOW, I had absolutely no idea that this code had any following at all until Github sent email notice that @Johnny512 had commented on it.

Thanks for all the feedback & encouragement. I've updated the code with the suggestions from @kinghelix, @trevormarburger, @miaozeyu and @SilverSurfer0

Cheers!

@Jarmahent

This comment has been minimized.

Copy link

@Jarmahent Jarmahent commented Sep 13, 2019

THANKS

@Ricks-Lab

This comment has been minimized.

Copy link

@Ricks-Lab Ricks-Lab commented Mar 23, 2020

Thanks! Used this in a covid-19 analytics project.

Should consider adding Guam: GU

@rogerallen

This comment has been minimized.

Copy link
Owner Author

@rogerallen rogerallen commented Mar 23, 2020

Updated per @Ricks-Lab and now matching https://en.wikipedia.org/wiki/Territories_of_the_United_States
+ American Samoa
+ Guam
- Palau (not a US Territory)
Now 50 states + 1 DC + 5 Territories (American Samoa, Guam, Northern Mariana Islands, Puerto Rico and U.S. Virgin Islands)

@Ricks-Lab

This comment has been minimized.

Copy link

@Ricks-Lab Ricks-Lab commented Mar 24, 2020

Updated per @Ricks-Lab and now matching https://en.wikipedia.org/wiki/Territories_of_the_United_States

  • American Samoa
  • Guam
  • Palau (not a US Territory)
    Now 50 states + 1 DC + 5 Territories (American Samoa, Guam, Northern Mariana Islands, Puerto Rico and U.S. Virgin Islands)

Based on this site, The abbreviation for Guam is GU.

@rogerallen

This comment has been minimized.

Copy link
Owner Author

@rogerallen rogerallen commented Mar 24, 2020

Fixed Guam = GU. Thanks for reviewing this, @Ricks-Lab!

@hombreRj

This comment has been minimized.

Copy link

@hombreRj hombreRj commented Mar 31, 2020

poggers

@sundayj

This comment has been minimized.

Copy link

@sundayj sundayj commented Jun 12, 2020

Thank you!

@sandlerj

This comment has been minimized.

Copy link

@sandlerj sandlerj commented Jun 21, 2020

@rogerallen you are a saint! thank you!

@aavetisyan24

This comment has been minimized.

Copy link

@aavetisyan24 aavetisyan24 commented Jul 9, 2020

for i in range 100:
print("Thank you bro!")

@M1Ergash

This comment has been minimized.

Copy link

@M1Ergash M1Ergash commented Aug 1, 2020

Great job, saves time

@jaeseokan94

This comment has been minimized.

Copy link

@jaeseokan94 jaeseokan94 commented Aug 2, 2020

Thanks!

@temidolan

This comment has been minimized.

Copy link

@temidolan temidolan commented Aug 12, 2020

Awesome! Thank you

@dubpirate

This comment has been minimized.

Copy link

@dubpirate dubpirate commented Aug 20, 2020

Thank you so much, I can't ever repay you.

@SagiPolaczek

This comment has been minimized.

Copy link

@SagiPolaczek SagiPolaczek commented Sep 19, 2020

Thanks 👍
Save me to write that manually!

@Miles-Murphy

This comment has been minimized.

Copy link

@Miles-Murphy Miles-Murphy commented Sep 25, 2020

Thank you so much! Have a great day :)

@ChrisShuttle

This comment has been minimized.

Copy link

@ChrisShuttle ChrisShuttle commented Sep 29, 2020

Thank you!

@baileywellen

This comment has been minimized.

Copy link

@baileywellen baileywellen commented Oct 2, 2020

saved me some time, thank you!

@tikhonova

This comment has been minimized.

Copy link

@tikhonova tikhonova commented Oct 30, 2020

aMAzing ;) Thank you!

@gerardovitale

This comment has been minimized.

Copy link

@gerardovitale gerardovitale commented Nov 14, 2020

This's greeeeeat!!

@KellyWemmer

This comment has been minimized.

Copy link

@KellyWemmer KellyWemmer commented Nov 17, 2020

Thank YOU!!

@pnojai

This comment has been minimized.

Copy link

@pnojai pnojai commented Nov 27, 2020

What a nice service to other developers. Makes me think of building a package with lots of lookups needed for cartography, like a FIPS lookup for example. Now, I bet someone else already did that, too.

@JSMboli

This comment has been minimized.

Copy link

@JSMboli JSMboli commented Jan 22, 2021

Thank for this.

I am trying to do something similar but worldwide.

I have list of locations that is mixed with states, cities and countries, counties and regions in abbreviations and some in full. For instance, NY, CA, England, UK, USA, Minnesota, London, Bradford, etc. I want it all to be converted to countries such as NY=USA, England=UK, Scotland = UK, Minnesota = USA, etc.

Is it possible to do this in python?
Thanks in advance.

@rmfranciacastillo

This comment has been minimized.

Copy link

@rmfranciacastillo rmfranciacastillo commented Feb 2, 2021

Sweet! You just save me a lot of time!

@kinghelix You are today's hero!

@fletcheaston

This comment has been minimized.

Copy link

@fletcheaston fletcheaston commented Feb 21, 2021

In case anyone needs these in the form of enums.

from enum import Enum
from functools import cache


class State(str, Enum):
    ALABAMA = "Alabama"
    ALASKA = "Alaska"
    AMERICAN_SAMOA = "American Samoa"
    ARIZONA = "Arizona"
    ARKANSAS = "Arkansas"
    CALIFORNIA = "California"
    COLORADO = "Colorado"
    CONNECTICUT = "Connecticut"
    DELAWARE = "Delaware"
    DISTRICT_OF_COLUMBIA = "District of Columbia"
    FLORIDA = "Florida"
    GEORGIA = "Georgia"
    GUAM = "Guam"
    HAWAII = "Hawaii"
    IDAHO = "Idaho"
    ILLINOIS = "Illinois"
    INDIANA = "Indiana"
    IOWA = "Iowa"
    KANSAS = "Kansas"
    KENTUCKY = "Kentucky"
    LOUISIANA = "Louisiana"
    MAINE = "Maine"
    MARYLAND = "Maryland"
    MASSACHUSETTS = "Massachusetts"
    MICHIGAN = "Michigan"
    MINNESOTA = "Minnesota"
    MISSISSIPPI = "Mississippi"
    MISSOURI = "Missouri"
    MONTANA = "Montana"
    NEBRASKA = "Nebraska"
    NEVADA = "Nevada"
    NEW_HAMPSHIRE = "New Hampshire"
    NEW_JERSEY = "New Jersey"
    NEW_MEXICO = "New Mexico"
    NEW_YORK = "New York"
    NORTH_CAROLINA = "North Carolina"
    NORTH_DAKOTA = "North Dakota"
    NORTHERN_MARIANA_ISLANDS = "Northern Mariana Islands"
    OHIO = "Ohio"
    OKLAHOMA = "Oklahoma"
    OREGON = "Oregon"
    PENNSYLVANIA = "Pennsylvania"
    PUERTO_RICO = "Puerto Rico"
    RHODE_ISLAND = "Rhode Island"
    SOUTH_CAROLINA = "South Carolina"
    SOUTH_DAKOTA = "South Dakota"
    TENNESSEE = "Tennessee"
    TEXAS = "Texas"
    UTAH = "Utah"
    VERMONT = "Vermont"
    VIRGIN_ISLANDS = "Virgin Islands"
    VIRGINIA = "Virginia"
    WASHINGTON = "Washington"
    WEST_VIRGINIA = "West Virginia"
    WISCONSIN = "Wisconsin"
    WYOMING = "Wyoming"


@cache
def get_state_enum(state: str) -> State:
    state_mappings = {
        "AL": State.ALABAMA,
        "AS": State.AMERICAN_SAMOA,
        "AK": State.ALASKA,
        "AZ": State.ARIZONA,
        "AR": State.ARKANSAS,
        "CA": State.CALIFORNIA,
        "CO": State.COLORADO,
        "CT": State.CONNECTICUT,
        "DC": State.DISTRICT_OF_COLUMBIA,
        "DE": State.DELAWARE,
        "FL": State.FLORIDA,
        "GA": State.GEORGIA,
        "GU": State.GUAM,
        "HI": State.HAWAII,
        "ID": State.IDAHO,
        "IL": State.ILLINOIS,
        "IN": State.INDIANA,
        "IA": State.IOWA,
        "KS": State.KANSAS,
        "KY": State.KENTUCKY,
        "LA": State.LOUISIANA,
        "ME": State.MAINE,
        "MD": State.MARYLAND,
        "MA": State.MASSACHUSETTS,
        "MI": State.MICHIGAN,
        "MN": State.MINNESOTA,
        "MP": State.NORTHERN_MARIANA_ISLANDS,
        "MS": State.MISSISSIPPI,
        "MO": State.MISSOURI,
        "MT": State.MONTANA,
        "NE": State.NEBRASKA,
        "NV": State.NEVADA,
        "NH": State.NEW_HAMPSHIRE,
        "NJ": State.NEW_JERSEY,
        "NM": State.NEW_MEXICO,
        "NY": State.NEW_YORK,
        "NC": State.NORTH_CAROLINA,
        "ND": State.NORTH_DAKOTA,
        "OH": State.OHIO,
        "OK": State.OKLAHOMA,
        "OR": State.OREGON,
        "PA": State.PENNSYLVANIA,
        "PR": State.PUERTO_RICO,
        "RI": State.RHODE_ISLAND,
        "SC": State.SOUTH_CAROLINA,
        "SD": State.SOUTH_DAKOTA,
        "TN": State.TENNESSEE,
        "TX": State.TEXAS,
        "UT": State.UTAH,
        "VT": State.VERMONT,
        "VA": State.VIRGINIA,
        "VI": State.VIRGIN_ISLANDS,
        "WA": State.WASHINGTON,
        "WV": State.WEST_VIRGINIA,
        "WI": State.WISCONSIN,
        "WY": State.WYOMING,
        **{enum.value: enum for enum in State},
    }

    return state_mappings[state]

Note: updated to include more readable enums (full state names instead of abbreviations), and added a mapper function to return enums from both full and abbreviated names. If you're using Python <3.9, you can replace the @cache decorator with @lru_cache.

@jrpope2014

This comment has been minimized.

Copy link

@jrpope2014 jrpope2014 commented Feb 26, 2021

Bumped into this while working on a Coursera Data Science class and looking for something of this form for a quick script, just wanted to be sure to say thanks! :)

@aidanchandra

This comment has been minimized.

Copy link

@aidanchandra aidanchandra commented Mar 3, 2021

Thank you!

@tnalbertson

This comment has been minimized.

Copy link

@tnalbertson tnalbertson commented Mar 24, 2021

Awesome! Thanks for the time saver :)

@Eventhisone

This comment has been minimized.

Copy link

@Eventhisone Eventhisone commented Apr 9, 2021

Here it is reversed and including DC, Northern Mariana Islands, Palau, Puerto Rico, the Virgin Islands, and US military base abbreviations.

us_state_abbrev = {
            'AL': 'Alabama',
            'AK': 'Alaska',
            'AZ': 'Arizona',
            'AR': 'Arkansas',
            'CA': 'California',
            'CO': 'Colorado',
            'CT': 'Connecticut',
            'DE': 'Delaware',
            'FL': 'Florida',
            'GA': 'Georgia',
            'HI': 'Hawaii',
            'ID': 'Idaho',
            'IL': 'Illinois',
            'IN': 'Indiana',
            'IA': 'Iowa',
            'KS': 'Kansas',
            'KY': 'Kentucky',
            'LA': 'Louisiana',
            'ME': 'Maine',
            'MD': 'Maryland',
            'MA': 'Massachusetts',
            'MI': 'Michigan',
            'MN': 'Minnesota',
            'MS': 'Mississippi',
            'MO': 'Missouri',
            'MT': 'Montana',
            'NE': 'Nebraska',
            'NV': 'Nevada',
            'NH': 'New Hampshire',
            'NJ': 'New Jersey',
            'NM': 'New Mexico',
            'NY': 'New York',
            'NC': 'North Carolina',
            'ND': 'North Dakota',
            'OH': 'Ohio',
            'OK': 'Oklahoma',
            'OR': 'Oregon',
            'PA': 'Pennsylvania',
            'RI': 'Rhode Island',
            'SC': 'South Carolina',
            'SD': 'South Dakota',
            'TN': 'Tennessee',
            'TX': 'Texas',
            'UT': 'Utah',
            'VT': 'Vermont',
            'VA': 'Virginia',
            'WA': 'Washington',
            'WV': 'West Virginia',
            'WI': 'Wisconsin',
            'WY': 'Wyoming',
            'DC': 'District of Columbia',
            'MP': 'Northern Mariana Islands',
            'PW': 'Palau',
            'PR': 'Puerto Rico',
            'VI': 'Virgin Islands',
            'AA': 'Armed Forces Americas (Except Canada)',
            'AE': 'Armed Forces Africa/Canada/Europe/Middle East',
            'AP': 'Armed Forces Pacific'
        }
@squeakyboots

This comment has been minimized.

Copy link

@squeakyboots squeakyboots commented Apr 20, 2021

Those east of the Mississippi River:
['AL', 'CT', 'DE', 'FL', 'GA', 'IL', 'IN', 'KY', 'ME', 'MD', 'MA', 'MI', 'MS', 'NH', 'NJ', 'NY', 'NC', 'OH', 'PA', 'RI', 'SC', 'TN', 'VT', 'VA', 'WV', 'WI']

@khurchla

This comment has been minimized.

Copy link

@khurchla khurchla commented May 28, 2021

Just what I need, cheers for creating this and sharing it!

@semvijverberg

This comment has been minimized.

Copy link

@semvijverberg semvijverberg commented Jun 4, 2021

Hereby an auto enumerator.

from enum import IntEnum, auto

class US_States(IntEnum):
    AL = auto() # Alabama
    AK = auto() # Alaska
    AS = auto() # American Samoa
    AZ = auto() # Arizona
    AR = auto() # Arkansas
    CA = auto() # California
    CO = auto() # Colorado
    CT = auto() # Connecticut
    DE = auto() # Delaware
    DC = auto() # District of Columbia
    FL = auto() # Florida
    GA = auto() # Georgia
    GU = auto() # Guam
    HI = auto() # Hawaii
    ID = auto() # Idaho
    IL = auto() # Illinois
    IN = auto() # Indiana
    IA = auto() # Iowa
    KS = auto() # Kansas
    KY = auto() # Kentucky
    LA = auto() # Louisiana
    ME = auto() # Maine
    MD = auto() # Maryland
    MA = auto() # Massachusetts
    MI = auto() # Michigan
    MN = auto() # Minnesota
    MS = auto() # Mississippi
    MO = auto() # Missouri
    MT = auto() # Montana
    NE = auto() # Nebraska
    NV = auto() # Nevada
    NH = auto() # New Hampshire
    NJ = auto() # New Jersey
    NM = auto() # New Mexico
    NY = auto() # New York
    NC = auto() # North Carolina
    ND = auto() # North Dakota
    MP = auto() # Northern Mariana Islands
    OH = auto() # Ohio
    OK = auto() # Oklahoma
    OR = auto() # Oregon
    PA = auto() # Pennsylvania
    PR = auto() # Puerto Rico
    RI = auto() # Rhode Island
    SC = auto() # South Carolina
    SD = auto() # South Dakota
    TN = auto() # Tennessee
    TX = auto() # Texas
    UT = auto() # Utah
    VT = auto() # Vermont
    VI = auto() # Virgin Islands
    VA = auto() # Virginia
    WA = auto() # Washington
    WV = auto() # West Virginia
    WI = auto() # Wisconsin
    WY = auto() # Wyoming
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment