-1

I need help coming up with a function in Python that takes 3 arguments that are lists and can return all the combinations.

For example, if I ran:

shirts = ['white', 'blue']
ties = ['purple', 'yellow']
suits = ['grey', 'blue']
combinations = dress_me(shirts, ties, suits)
for combo in combinations:
    print combo

It would print something like:

('white', 'purple', 'grey')
('white', 'purple', 'blue')
('white', 'yellow', 'grey')
('white', 'yellow', 'blue')
('blue', 'purple', 'grey')
('blue', 'purple', 'blue')
('blue', 'yellow', 'grey')
('blue', 'yellow', 'blue')
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Zaid Haddadin
  • 31
  • 1
  • 2
  • I think what I have answered is right. – zhangyangyu Aug 08 '13 at 07:12
  • Welcome to Stack Overflow. Please read the [About] page soon. In general, avoid trying to write your entire question in the title. – Jonathan Leffler Aug 08 '13 at 07:14
  • @Jonathan : I hate when some nerds will downvote this post , I mean this guy is new , give him some break -- no you have to read 40 page of EULA , and then post here . – motiur Aug 08 '13 at 07:16
  • 1
    Nobody downvoted it though actually it deserves it. The OP has just posted a same question. I and some others have answered it. If he is not satisfied with the answers. He may comments edit.@MotiurRahman – zhangyangyu Aug 08 '13 at 07:22

3 Answers3

6

itertools to the rescue.

import itertools

def dress_me(*choices):
  return itertools.product(*choices)
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1
def dress_me(l1, l2, l3):
    return [(i, j, k) for i in l1 for j in l2 for k in l3]
BlackMamba
  • 10,054
  • 7
  • 44
  • 67
0
def dress_me(l1, l2, l3):
    res = []
    for i in l1:
        for j in l2:
            for k in l3:
                res.append((i, j, k))
    return res

shirts = ['white', 'blue']
ties = ['purple', 'yellow']
suits = ['grey', 'blue']

if __name__ == '__main__':  
    combinations = dress_me(shirts, ties, suits)
    for combo in combinations:
        print(combo)
BlackMamba
  • 10,054
  • 7
  • 44
  • 67