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',
'Arizona': 'AZ',
'Arkansas': 'AR',
'California': 'CA',
'Colorado': 'CO',
'Connecticut': 'CT',
'Delaware': 'DE',
'District of Columbia': 'DC',
'Florida': 'FL',
'Georgia': 'GA',
'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',
'Palau': 'PW',
'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')
@skillachie

This comment has been minimized.

Copy link

skillachie commented May 15, 2013

Thanks. Saved me some time

@dalguji

This comment has been minimized.

Copy link

dalguji commented Apr 19, 2014

Awesome. Thanks!

@lwgray

This comment has been minimized.

Copy link

lwgray commented Jul 12, 2015

Thanks save me a lot of time!

@jameserrico

This comment has been minimized.

Copy link

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 commented Dec 29, 2015

Thanks!

@JiahaoShan

This comment has been minimized.

Copy link

JiahaoShan commented Apr 27, 2016

THANKS!

@kirkhunter

This comment has been minimized.

Copy link

kirkhunter commented May 13, 2016

You da real mvp

@jimioke

This comment has been minimized.

Copy link

jimioke commented Jan 5, 2017

Thanks, haha!

@drgrubman

This comment has been minimized.

Copy link

drgrubman commented Jan 9, 2017

Simple and incredibly helpful!

@alexroussos

This comment has been minimized.

Copy link

alexroussos commented Jun 2, 2017

🥇

@slowjazz

This comment has been minimized.

Copy link

slowjazz commented Jun 21, 2017

Thank you for this

@gassnerj

This comment has been minimized.

Copy link

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 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 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 commented Nov 10, 2017

Thanks for your great work!

@juan1393

This comment has been minimized.

Copy link

juan1393 commented Dec 7, 2017

Thanks!

@ajankuv

This comment has been minimized.

Copy link

ajankuv commented Jan 14, 2018

thanks, saved my ass!

@kinghelix

This comment has been minimized.

Copy link

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 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.

@dixieanalytics

This comment has been minimized.

Copy link

dixieanalytics commented Oct 29, 2018

Awesome, thank you!

@TomFaulkner

This comment has been minimized.

Copy link

TomFaulkner commented Jan 18, 2019

Saved me some time as well, thanks.

@trevormarburger

This comment has been minimized.

Copy link

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 commented Jul 8, 2019

Thank you for this.

@rogerallen

This comment has been minimized.

Copy link
Owner Author

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 commented Sep 13, 2019

THANKS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.