this is another pretty simple question, but i've been trying to do it, and cant get it

basically what i need is pretty simple, its like say you have a list of 5 numbers. now you need to print out all the lists of possible 3 numbers from those 5 numbers. order doesn't matter (so like [3,4,5] won't be relisted as [4,5,3])

but basically this is what i had in mind but of course it gives an error... if anybody knows any simpler ways please do tell.

d = [3,6,6,2,7]
b = [6,8]
h = []
for n in range (5):
    d.pop [n]
    if len(d) is 5:
        for i in range (4):
            d.pop [i]
            h = d + b
            print (h)
9 Years
Discussion Span
Last Post by max.yevs

actually sorry line 6 "if len(d) is 5:" is not supposed to be there i had it left over from something else

but it does not work anyways because i can't do d.pop[n] unfortunately, i have to put an integer instead of n only


oh ok i think i got it... it has all the combinations, only problem is it relists some a couple times

d = [1,2,3,4,5]
h = []
for n in range (5):
    x = d[n]
    for i in range (5):
        if i is not n:
            y = d[i]
            d.remove (x)
            d.remove (y)
            print (d)
            d.insert (i,y)
            d.insert (n,x)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.