473,487 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

walking a list

Hi all,

I have this little question, basicly i solved it already by writing a
little bit of code for it (using recursion), but still i am wondering if
there is a shorter ways to do things (like 1 or 2 commands).

the problem is this, imagine i have a list:

list = [1, 2, [3, 2], 5, [6, 5, 4]]

if i print out this list using 'for element in list: print element,'
it will show me the following:

1
2
[3, 2]
5
[6, 5, 4]

but what i really want to show is:

1
2
3
2
5
6
5
4

What i want to do is run through the list and when i get back a list run
through that list as well (and if that list contains a list run through it
again etc.).

all suggestions are welcome, i'm ready to learn from the pro's ;)

--
One monk said to the other, "The fish has flopped out of the net! How will it
live?" The other said, "When you have gotten out of the net, I'll tell you."

Jul 18 '05 #1
2 3368
def print_or_recurse(l):
for i in l:
if isinstance(i, list): print_or_recurse(i)
else: print i

print_or_recurse([1,2,[3,2],5,[6,5,4]])

Jeff

Jul 18 '05 #2
On Fri, 23 Apr 2004 13:21:43 -0500, Jeff Epler wrote:
if isinstance(i, list): print_or_recurse(i)


great! thanks, i didn't know 'isinstance' existed.
what i did was the following:

if 'list' in str(type(list)):

it worked as well :)

--
One monk said to the other, "The fish has flopped out of the net! How will it
live?" The other said, "When you have gotten out of the net, I'll tell you."

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9709
by: Who Cares | last post by:
Okay, this may be a bit of a newbie question but I could really use some help. I want to write a set of routines that will read in the contents of XML files. I'll be reading the tag names,...
0
1051
by: Chris Lyon | last post by:
I handle a lot of audio files of different formats, which live in a directory tree. WAV files in a wav directory, AIFF's in an AIFF directory next to the wav directory and mp3's in an MP3 directory...
2
2820
by: David Stockwell | last post by:
snippet payload = {} try: message = email.message_from_string(message) messageHeader = dict(message._headers) addressFrom = messageHeader addressReply = messageHeader.get("Reply-To")...
2
1260
by: Jeff Elkins | last post by:
I'm writing a small wxpython app to display and update a dataset. So far, I get the first record for display: try: cursor = conn.cursor () cursor.execute ("SELECT * FROM dataset") item =...
1
2776
by: Zachary Hartnett | last post by:
I was trying to write a routine this morning that would open a given assembly, walk the inheritance tree of classes in the assembly, and provide a list of classes in the assembly that inherit from...
5
4581
by: Allerdyce.John | last post by:
In STL list, is it safe to do this: list<A> aList; //private attribute of MyClass void MyClass:: aMethod() { list<A>::Iterator iter; for ( iter = aList.begin() ; iter != aList.end() ;...
5
1736
by: TrulyUnusualdotcom | last post by:
I'm reading PHP & MySQL for Dummies 2nd edition...ya ya I know..lame. Anyway I got to the part about walking through an array and I just can't seem to figure out what this would be used for. What...
8
2092
by: Ben Hallert | last post by:
Hi guys! I'm working on a little javascriptlet/greasemonkey script, and I've run into a challenge that I think can be solved with walking the DOM, but I feel like I'm kludging my way through and...
2
1312
by: Evan Carmi | last post by:
hi, i am creating a program to go through a directory structure recursively (including directories below it) and move all files that end in .msf to a directory above the current level. the...
0
7106
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.