get all possible combinations of a list’s elements in python
April 16th, 2008 by Lawrence David
let’s say you’ve got a list of the numbers 0 through 4 and you wanted all 32 possible combinations of them. Â to get that list:
list_of_five = range(0,5)  Â
swap_list_list = [[]]
for swap in list_of_five:
   temp_lists = []
   for list_stub in swap_list_list:
     this_list = copy.copy(list_stub)
     this_list.append(swap)
     temp_lists.append(this_list)
temp_lists.append(list_stub)
   swap_list_list = temp_lists
print swap_list_list
Â
Â
After doing “import copy” to your code, I get this output.
$ python combo.py
[[4], []]
[…] found some code (by googling) that apparently does what I’m looking for, but I found the code fairly opaque […]
Yes. You got this wrong.
When you don’t have import copy module this is completely useless.
[…] found some code (by googling) that apparently does what I’m looking for, but I found the code fairly opaque […]
[…] 코드를 찾았 습니다 ì°¾ê³ ìžˆëŠ” ê²ƒì„ ë¶„ëª…ížˆí•˜ëŠ” (구글ë§ì— ì˜í•´)를 찾았지만 코드가 ìƒë‹¹ížˆ ë¶ˆíˆ¬ëª…í•˜ê³ ì‚¬ìš©í•˜ëŠ” 것ì—주ì˜ë¥¼ 기울였습니다. ë˜í•œ ë” ìš°ì•„í•œ ì†”ë£¨ì…˜ì´ ìžˆì–´ì•¼í•œë‹¤ê³ ìƒê°í•©ë‹ˆë‹¤. […]
I savour, cause I found exactly what I used to be looking for.
You’ve ended my four day long hunt! God Bless you man. Have
a great day. Bye