2

I want to create a dictionary that stores the full name of the 50 states in the word, and the abbreviations in the value, given a list of the names and abbreviations. I am expecting a dictionary like {'Alabama' : 'AK', 'Alaska': 'AL', ...}. I've tried

state_to_abbrev = {} for word in states: for i in range(50): state_to_abbrev[word] = states[i] state_to_abbrev[word] = abbreviations[i] state_to_abbrev 

And I'm getting {'Alabama': 'WY', 'Alaska': 'WY', 'Arizona': 'WY', 'Arkansas': 'WY', 'California': 'WY', 'Colorado': 'WY', 'Connecticut': 'WY', 'Delaware': 'WY', 'Florida': 'WY', 'Georgia': 'WY', 'Hawaii': 'WY', .....}

2
  • Looks like relatively straightforward array access. Verify that the content of abbreviations is what you expect. Commented Mar 12, 2018 at 2:30
  • You have an unnecessary loop here -- for word in states means for each of the 50 states in your state list, loop through an index of 50 and assign the value of that state's name with state[i] then immediately overwrite it with abbreviation[i] Commented Mar 12, 2018 at 2:35

5 Answers 5

2

You can try:

state_to_abbrev = {} for word in states: for i in range(50): state_to_abbrev[states[i]] = abbreviations[i] state_to_abbrev 

Update:

As suggested in comment, you don't need extra loop for word, you can simply try:

state_to_abbrev = {} for i in range(50): state_to_abbrev[states[i]] = abbreviations[i] state_to_abbrev 

Then, using dict comprehension you can assign in single line for above loop:

state_to_abbrev = {states[i]:abbreviations[i] for i in range(50)} 

Also, since you are using two lists, you can try using zip, you can also look for example in documentation:

dict(zip(states,abbreviations)) 
Sign up to request clarification or add additional context in comments.

4 Comments

... why the nested loop?
You have an extra loop like the OP, see my comment
Oops, I just tried fixing the code, I will edit it thanks!
I think zip method is the best answer overall
0

Try enumerate

state_to_abbrev = {} for i, word in enumerate(states): state_to_abbrev[word] = abbreviations[i] state_to_abbrev 

Comments

0

The naive way to do what you are trying to do is the following:

state_to_abbrev = {} for i in range(len(states)): state_to_abbrev[states[i]] = abbreviations[i] 

You have instead a nested loop, but that's not what you want, the nested loop loops over the cartesian product of your two lists, i.e.:

In [46]: for a in ('a','b'): ...: for n in (1, 2): ...: print(a, n) ...: a 1 a 2 b 1 b 2 In [47]: 

But really, in Python, instead of using indices, you'd use zip to iterate over two lists "in parallel":

state_to_abbrev = {} for st, abbr in zip(states, abbreviations): state_to_abbrev[st] = abbr 

But really, you should know that the dict constructor takes an iterable of key-value pairs already, so what you really want is:

state_to_abbrev = dict(zip(states, abbreviations)) 

Comments

0

Here's a 1-liner version: (Python 3)

state_to_abbrev = dict(zip(states,abbreviations)) print(state_to_abbrev) 

Prints out:

{'Alabama': 'AK', 'Alaska': 'AL', ...} 

Comments

0

You can try anyone of these method :

states=['Alabama','Arizona'] abbreviations=['AK','AL'] print(dict([i for i in zip(states,abbreviations)])) print({i[0]:i[1] for i in zip(states,abbreviations)}) 

output:

{'Alabama': 'AK', 'Arizona': 'AL'} {'Alabama': 'AK', 'Arizona': 'AL'} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.