2

a script is feeding me with list of list of list or list of list. What I plan to do is call this

test = myList[0][0][0]

and if an exception is raised I'll know that it's a list of list.

Is there a better/proper way to do this?

Thanks.

sliders_alpha
  • 2,276
  • 4
  • 33
  • 52

1 Answers1

2

I'm not sure if it's better/proper, but you can also test whether something is a list with isinstance or type functions.

For example

a = [1,2,3]
b = (1,2,3) # Not a list

type(a) == type([])   # True
type(b) == type([])   # False

type(a) is list       # True
type(b) is list       # False

isinstance(a, list)   # True
isinstance(b, list)   # False

The first method is probably not ideal, the second would probably be better if you were to use type, but I think the general consensus is that isinstance is generally better.

EDIT: Some discussion about the difference between the two approaches

So, I guess your code would look something like:

if(isinstance(myList[0][0], list)):
    # Use myList[0][0][0]
else:
    # Use myList[0][0]
Community
  • 1
  • 1
jedwards
  • 29,432
  • 3
  • 65
  • 92
  • handling exceptions in Python is much faster that checking for type. besides, "It's Better to Beg for Forgiveness than to Ask for Permission" – newtover Mar 22 '13 at 08:33
  • @Newtover, even if that's true (I've never heard that and [this](http://stackoverflow.com/questions/5589532/try-catch-or-validation-for-speed) would indicate otherwise -- My guess is that the benefit relies on how many times an exception will be thrown), catching an exception seems too ham-fisted for something as straight forward as this. – jedwards Mar 22 '13 at 08:41
  • I think I'll go for your method, from what I seen (in the professional world) an exception must only be raised when there is no other way at all. – sliders_alpha Mar 22 '13 at 08:54
  • It's definitely a debated topic. One thing that might push you one way or the other is how often you expect the input to be malformed. If it's very infrequently, for performance-critical apps, then the overhead of unnecessarily checking types might not be worth it. And catching an exception in the rare case of malformed input might be faster. Usually I'd consider worrying about that premature optimization though. Additionally, explicitly testing assures you that what you're dealing with is, in fact, a list. Just because it "doesn't fail" doesn't mean it's what you expect. – jedwards Mar 22 '13 at 09:01
  • Look here, http://stackoverflow.com/questions/598157/cheap-exception-handling-in-python. Moreover you are checking for an implementation, but actually interested in an interface (http://en.wikipedia.org/wiki/Duck_typing) – newtover Mar 22 '13 at 09:08
  • Determining whether a key exists in a dictionary is *much* more involved than checking a type which is known at runtime. And if you read the linked blog post, you'll notice the author comes to a conclusion similar to my previous comment -- basically, it depends on the frequency of the "malformation". – jedwards Mar 22 '13 at 09:12