5
l = [True, True , False]

Without using itertools module.

How do yo create permutations of l in a new list

newlist = [[True,True,False],[True,True,True], [False,False,True],[False,False,False]....]

essentially this is what im trying to do:

allorderings = itertools.product ([False, True], repeat = n)
user3349106
  • 77
  • 1
  • 7
  • http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python – seb Apr 08 '14 at 06:15
  • [True, True, True], [False, False, True] and [False, False, False] are not permutations of [True, True, False] – miles82 Apr 08 '14 at 06:17
  • oh right. thats not called permutations. what do you call different combinations? – user3349106 Apr 08 '14 at 06:19
  • So you want product but your input list wasn't specified correctly. Have a look at the implementation here: https://docs.python.org/2/library/itertools.html#itertools.product – miles82 Apr 08 '14 at 06:28

4 Answers4

4

Use itertools.permutations

import itertools
l = [True, True , False]
newlist = list(itertools.permutations(l))

EDIT: from your question, one of the permutations you need is (True, True, True) which is not a permutation at all of the list l. This answer gives you permutations of a list in the technical sense and you may have to do extra work to achieve what you show in the question(unless of course that was a typo).

yati sagade
  • 1,355
  • 9
  • 24
2

The simplest way I could think of, would be to iterate over the same list of items thrice and collect only the unique items, like this

l = set([True, True, False])
print {(i, j, k) for i in l for j in l for k in l}
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1

Use itertools.permutations's equivalent in pure python from the official docs?

bravmi
  • 505
  • 2
  • 7
  • 13
0

try this ,

>>> import itertools
>>> print list(itertools.permutations([True, True , False], 3))

Output

[(True, True, False), (True, False, True), (True, True, False), (True, False, True), (False, True, True), (False, True, True)]

OR try this,

>>> def combiList(l):
        if not l:
            return [[]]
        res = []
        for e in l:
            temp = l[:]
            temp.remove(e)
            res.extend([[e] + r for r in combiList(temp)])

        return res


>>> combiList([True, True , False])
[[True, True, False], [True, False, True], [True, True, False], [True, False, True], [False, True, True], [False, True, True]]
>>>
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81