Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all permutations without replacement?

I'd like to get all the permutations of a list but without any repetitions, regardless of ordering. It's kind of hard to describe so I'll give an example. I'd really like to know the name of this operation because I use it all the time. Also a simple way to achieve this in python would really help me out. Thanks!

e.g

['foo', 'bar', 'la']

==>

['foo', 'bar']
['foo', 'la']
['ba', 'la']
like image 786
Tim Avatar asked Dec 15 '22 23:12

Tim


1 Answers

Using itertools.combinations:

>>> import itertools
>>> list(itertools.combinations(['foo', 'bar', 'la'], 2))
[('foo', 'bar'), ('foo', 'la'), ('bar', 'la')]
like image 59
falsetru Avatar answered Dec 28 '22 07:12

falsetru