Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a value in a Python Dictionary have two values?

For a test program I'm making a simple model of the NFL. I'd like to assign a record (wins and losses) to a team as a value in a dictionary? Is that possible?

For example:

afcNorth = ["Baltimore Ravens", "Pittsburgh Steelers", "Cleveland Browns", "Cincinatti Bengals"]

If the Ravens had 13 wins and 3 loses, can the dictionary account for both of those values? If so, how?

like image 418
Zack Shapiro Avatar asked Jan 29 '11 23:01

Zack Shapiro


2 Answers

sure, just make the value a list or tuple:

afc = {'Baltimore Ravens': (10,3), 'Pb Steelers': (3,4)}

If it gets more complicated, you might want to make a more complicated structure than a tuple - for example if you like dictionaries, you can put a dictionary in your dictionary so you can dictionary while you dictionary.

afc = {'Baltimore Ravens': {'wins':10,'losses': 3}, 'Pb Steelers': {'wins': 3,'losses': 4}}

But eventually you might want to move up to classes...

like image 112
Spacedman Avatar answered Oct 24 '22 06:10

Spacedman


The values in the dictionary can be tuples or, maybe better in this case, lists:

d = {"Baltimore Ravens": [13, 3]}
d["Baltimore Ravens"][0] += 1
print d
# {"Baltimore Ravens": [14, 3]}
like image 22
Sven Marnach Avatar answered Oct 24 '22 06:10

Sven Marnach