Mateusz Sawicki
Mateusz Sawicki

Reputation: 25

Loop for combination python

I need a loop for combination of for example: i got 2 lists:

list = [1,2,3]
combiantion_list = [(1), (2), (3), (1,2), (2,3), (1,3), (1,2,3)]

I want to a loop to get to all these lists in my combination_list, for example:

combination_list[0][0]
combination_list[1][0]
combination_list[2][0]
combination_list[3][0]
combination_list[3][1]
etc...

Upvotes: 0

Views: 218

Answers (3)

Loocid
Loocid

Reputation: 6451

You are after something like this:

for i in combiniation_list:
    for j in i:
        #do what you want with j

If you have any specific questions as to how it works, let me know.

>>> combination_list = [(1,2),(3,4)]
>>> for i in combination_list:
...     for j in i:
...             print(j)
...
1
2
3
4

Upvotes: 2

Mateusz Sawicki
Mateusz Sawicki

Reputation: 25

I got the combination_list already... All i need now is to get through all those elements inside that combination_list (including lists inside - for example: not giving (1,2), (1,3), (2,3) -> i want also to get inside these lists)

Upvotes: 0

Andrey
Andrey

Reputation: 60085

from itertools import chain, combinations
list(chain.from_iterable(combinations([1,2,3], x) for x in xrange(3)))

Upvotes: 1

Related Questions