0

I apologise if this is a duplicate question but I could not find any similar example anywhere so seeking your help.

lookup = {}

# These values will be filled by DB lookup service at runtime
# Maximum array length for category is unknown before program start 
# Format [Lookup Category], [Lookup Key], Lookup Id

lookup['name']['John'] = 1
lookup['name']['Jane'] = 2
lookup['name']['Joe'] = 3
lookup['gender']['Male'] = 1
lookup['gender']['Female'] = 2
lookup['country']['Japan'] = "jp"
lookup['country']['China'] = "ch"

print lookup['name']['Jane']
print lookup['gender']['Female']
print lookup['country']['China']
1
  • This question is extremely vague... you seem to have some sort of problem involving nested dictionaries, but that's all I can currently grasp from it. Commented Jul 29, 2013 at 11:09

1 Answer 1

6

You probably want collections.defaultdict

e.g.

from collections import defaultdict

lookup = defaultdict(dict)
lookup['name']['John'] = 1
lookup['name']['Jane'] = 2
lookup['name']['Joe'] = 3
lookup['gender']['Male'] = 1
lookup['gender']['Female'] = 2
lookup['country']['Japan'] = "jp"
lookup['country']['China'] = "ch"

print lookup['name']['Jane']
print lookup['gender']['Female']
print lookup['country']['China']
Sign up to request clarification or add additional context in comments.

2 Comments

Amazing. This is exactly what I was looking for. Appreciate quick help. Thank you again.
@user2629996 Remember to mark the question as accepted using the green tick. Enjoy your day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.