Any way to use a range as a key in a dictionary?

Carl Banks pavlovevidence at gmail.com
Thu Mar 26 22:45:51 EDT 2009


On Mar 26, 2:35 pm, Mudcat <mnati... at gmail.com> wrote:
> I would like to use a dictionary to store byte table information to
> decode some binary data. The actual number of entries won't be that
> large, at most 10. That leaves the other 65525 entries as 'reserved'
> or 'other' but still need to be somehow accounted for when
> referenced.


I seems like all you really need it to use the get method.  If the
item doesn't exist in the dictionary it returns None (or whatever you
pass in as the optional second argument).  For instance:


d = { 1: "s", 56: "w", 4363: "n", 8953: "k" }

d.get(1) -> "s"
d.get(56) -> "w"
d.get(10) -> None
d.get(10,"x") -> "x"


Carl Banks



More information about the Python-list mailing list