Wolverine
Wolverine

Reputation: 11

Python Dict Key with Multiple Values

I'm learning about dictionaries, and I know I could do this by creating two more dictionaries, but for brevity's sake, I wanted to make the code a bit more concise than having to create two more dict (granted, in the end, everything I wrote out for this dict would have to be written in the other dicts), so I guess I'm more curious than anything. And I know that I shouldn't be using a for-loop, but I don't know another way, and as far as LPTH sucking, I had to start somewhere.

question 1: How can I get this to cycle through each state/abbrev/city? Would I have to just make multiple dicts, or a series of lists and use those? question2: Why does this code run the for-loop 11 times?

states1 = {
    '1': 'Oregon', 'a': 'OR','population': '1000',
    '2': 'Florida', 'b': 'MI', 'population': '1000',
    '3': 'California', 'c': 'CA', 'population': '1000',
    '4': 'NewYork', 'd': 'NY', 'population': '1000',
    '5': 'Michigan','e': 'MI','population': '1000',
}

for city, pop, in states1.items():
    print "%s has the abbreviation %s and a population of %s" %states1['1'], states1['a'], states1['population'])

Upvotes: 0

Views: 829

Answers (5)

Wolverine
Wolverine

Reputation: 11

Just saw the last post, that makes a lot of sense. As far as how this could be useful, I don't know, it was just something I wanted to do, have three values to one key. I'm sure in the future I could take the info from a website or database and have it create pairs, like actors to characters played, but maybe not. It was just something I was annoyed I couldn't figure out on my own.

Upvotes: 0

user4642224
user4642224

Reputation: 167

Another example of how you could structure your data depending on your needs.

states1 = {
'Oregon': {'abrv': 'OR','population': '1000'},
'Florida':{ 'abrv': 'MI', 'population': '1000'},
'California':{'abrv': 'CA', 'population': '1000'},
'NewYork':{'abrv': 'NY', 'population': '1000'},
'Michigan':{'abrv': 'MI','population': '1000'}}



for state in states1:
  print "%s has the abbreviation %s and a population of %s" %(state, states1[state]['abrv'], states1[state]['population'])

Upvotes: 0

Wolverine
Wolverine

Reputation: 11

When I run it as you recommended me to (below), it spits out

Michigan has the abbreviation MI and a population of 1000
Michigan has the abbreviation MI and a population of 1000
Michigan has the abbreviation MI and a population of 1000

not sure why it's continually doing that, I would have guessed if it was for some reason to choose a totally random state because it's a dictionary that it would change the state each time, with occasional duplicates.

states1 = {
    'state': 'Oregon', 'abbrev': 'OR','population': '10000',
    'state': 'Florida', 'abbrev': 'MI', 'population': '1000',
    'state': 'California', 'abbrev': 'CA', 'population': '1000',
    'state': 'NewYork', 'abbrev': 'NY', 'population': '1000',
    'state': 'Michigan','abbrev': 'MI','population': '1000',
}

for city, pop, in states1.items():
    print "%s has the abbreviation %s and has a population of %s" % (states1['state'], states1['abbrev'], states1['population'])

Upvotes: 0

SirSteel
SirSteel

Reputation: 153

You could make each value be a list.

I do not understand what exactly you want to achieve, but the easy way would be:

states1 = {
1: ["Oregon", "OR", 1000]
2: ["Florida", "FL", 1000]
3: ["Pythonville", "PY", 1000]
4: ["Dictville", "DI", 1000]
}

Another thing you could look up is named tuple, it is one of my favorite data structures for when you want to quickly organize data.

from collections import namedtuple

CityInfo = namedtuple("CityInfo", ["Name", "Abbr", "Pop"])

states1 = {
            1: CityInfo("Oregon", "OR", 1000),
            2: CityInfo("Florida", "FL", 1000),
            3: CityInfo("Pythonville", "PY", 1000),
            4: CityInfo("Dictville", "DI", 1000)
            }

for city in states1.values():
    print("{} has the abbreviation {} and a population of {}".format(city.Name, city.Abbr, city.Pop))

Upvotes: 0

mgilson
mgilson

Reputation: 310287

Ideally, you'd have a homgonous datastructure -- e.g. a list of dict1. Each dict would hold the information about a particular state:

states1 = [
  {'name': 'Oregon', 'abbrev': 'OR', 'population': 1000},
  {'name': 'Florida', 'abbrev': 'FL', 'population': 1000},
  ...,
]

Now your loop looks like this:

for state in states1:
    print print "%s has the abbreviation %s and a population of %s" % (state['name'], states['abbrev'], states['population'])

The benefit here is hopefully clear. For any given state, you retrieve the related information the same way (the state's name is always reachable via the 'name' key).

If you ever need a mapping of state names (e.g. you want to look up a bunch of state's populations by their abbreviation), you can do that pretty easily by creating a dict...

abbrev_to_state = {state['abbrev']: state for state in states1}
florida_data = abbrev_to_state['FL']
new_hampshire_data = abbrev_to_state['NH']
...

1There are other options here too... You could use a custom class, but this also seems like a good candidate for a list of collections.namedtuple assuming you aren't planning on mutating the data.

Upvotes: 1

Related Questions