Flatten... or How to determine sequenceability?

Roman Suzi rnd at onego.ru
Fri May 25 10:59:58 EDT 2001


On Fri, 25 May 2001, Noel Rappin wrote:

> I'm writing code that needs to flatten a multi-dimension list or tuple into 
> a single dimension list.
> 
> [1, [2, 3], 4] => [1, 2, 3, 4]
> 
> As part of the algorithm, I need to determine whether each object in the 
> list is itself a sequence or whether it is an atom.  Using the types module 
> would cause me to miss any sequence-like object that isn't actually the 
> basic type.  So, what's the best (easiest, most foolproof) way to determine 
> whether a Python object is a sequence?

What is your definition of sequence-object?

Probably you could check if 

try:
  s[0]
  # sequence
except:
  # not sequence

> Thanks,
> 
> Noel Rappin


Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 





More information about the Python-list mailing list