Hello, I am new to Python and would be verry happy if someone would give me the code for a function that yields all sublists of a list. I need it for later implemetation in C, where I have to do work on these sublists.

I would need something like this:

def printSubs(l):
    for p in sublists(l):
        print p        # just for test

def sublists(l):
    # now this is the part I can't figure out :(

So the result of printSubs([1,2,3])
would be as follows
[1]
[2]
[3]
[1,2]
[1,3]
[2,3]
[1,2,3]

The order is not important!

Thanks for any help!

"So the result of printSubs([1,2,3])"
You would increment a counter or use a for() loop in the range of 0-->len(list_passed) to append an increasing number of items to the "sublist_list" based on another counter or for() loop. So the first pass would append one/each individual item, the second pass would append items 0 and 1, then 1 and 2, etc., the third pass would append 3 items each, until this counter is greater than the length of the list. Perhaps someone has a more elegant solution. This brute force method will work, and if the list isn't huge, won't take all that long.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.