Using multiple keys for one value in a Python dictionary -


i have python dictionary uses integers keys

d[7] = ... 

to reference custom objects:

c = car() d[7] = c 

however, each of these custom objects has string identifier (from third party). want able access objects using both integer or string. preferred way use both keys in same dictionary?

d[7] = c d["uabdskmakclsa"] = c 

or should split 2 dictionaries? or there better way?

it depends on you're doing.

if different kinds of keys different sources, know kind you're looking up, makes more sense, conceptually, use separate dictionaries.

on other hand, if need able handle keys either kind, it's simpler use single dictionary. otherwise, need write code uses type-switching, or tries 1 dict , tries other on keyerror, or else ugly.

(if you're worried efficiency, won't make difference either way. it's very, tiny bit faster things in 5000-key dictionary in 10000-key dictionary, , costs small of memory keep 2 5000-key dictionaries 1 10000-key dictionary. so, don't worry efficiency; whichever makes sense. don't have reason believe are worried efficiency, lot of people ask questions seem be, i'm covering bases.)


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -