ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] Flattening multi-dimentional list
by Pujo Aji other posts by this author
Sep 29 2005 12:54AM messages near this date
[Tutor] Flattening multi-dimentional list | Re: [Tutor] Flattening multi-dimentional list
Hi,
 Use this:

def _flatten(seq,myhasil):
  for isi in seq:
    if type(isi) != str:
      try:
        _flatten(isi,myhasil)
      except:
        myhasil.append(isi)
    else:
      myhasil.append(isi)

def flatten(seq):
  '''code to flatten tupple'''
  hasil = []
  _flatten(seq,hasil)
  return hasil

http://www.uselesspython.com/showcontent.php?author=38

Cheers,
pujo

 On 9/29/05, Bernard Lebel <3dbernard@[...].com>  wrote:
> 
>  Hello,
> 
>  I have this list wich is made of tuples. I wish I could "flatten" this
>  list, that is, to extract each element in the tuples and build a new
>  flat list with it. Is there any shortcut to do that or do I have to go
>  through some list comprehension-like procedure?
> 
>  (I have looked at sets but I have to keep things ordered).
> 
> 
>  Thanks
>  Bernard
>  _______________________________________________
>  Tutor maillist - Tutor@[...].org
>  http://mail.python.org/mailman/listinfo/tutor
> 
Attachments:
unknown1
unknown2
unknown3
unknown4

Thread:
Bernard Lebel
Pujo Aji
Kent Johnson
Alan G
Bernard Lebel
Danny Yoo
Bernard Lebel

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved