Ask Your Question


asked 2010-12-15 02:59:52 -0500


I need to store more data, and in order to do so, I would need multi-dimensional version of a dictionary... For example, let m be an object such that if you give the name and the age, such as m [Cathy, 19], then it returns the phone number. (In a dictionary you have one variable, and now I must call the data by more variables...) Do you know such a command?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2010-12-15 03:16:19 -0500

Jason Bandlow gravatar image

If I correctly understand your question, dictionaries can do this. You just need to use tuples as keys. Example:

sage: m = {} # an empty dictionary
sage: m[('Cathy', 19)] = '555-1212'
sage: m[('Cathy', 19)]
edit flag offensive delete link more


Thank you very much! (I also thought a similar thing but with lists in the argument. But I prefer tuples :-) )

Katika gravatar imageKatika ( 2010-12-15 04:05:50 -0500 )edit

You're welcome! In fact, lists won't work as keys. Look up 'mutable' vs. 'immutable' in connection with Python to understand why.

Jason Bandlow gravatar imageJason Bandlow ( 2010-12-15 06:43:38 -0500 )edit

You can also construct the tuples "implicitly" by doing m['Cathy', 19] = '555-1212' .

Mike Hansen gravatar imageMike Hansen ( 2010-12-15 07:45:48 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2010-12-15 02:59:52 -0500

Seen: 75 times

Last updated: Dec 15 '10