How to use list as key of dictionary?

Dustan DustanGroups at gmail.com
Tue Nov 6 11:08:49 CET 2007


On Nov 6, 3:58 am, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> Wildemar Wildenburger <lasses_w... at klapptsowieso.net> wrote:
> > maybe something like this could help:
>
> > def tupleize(non_tuple):
> >      try:
> >          return tuple(tupleize(thing) for thing in non_tuple)
> >      except TypeError:
> >          # non_tuple is not iterable
> >          return non_tuple
>
> Just don't try passing that a string or anything containing a string.

Untested

def tupleize(non_tuple):
     if isinstance(non_tuple, str):
          return non_tuple
     try:
          return tuple(tupleize(thing) for thing in non_tuple)
     except TypeError:
          # non_tuple is not iterable
          return non_tuple




More information about the Python-list mailing list