Python Forum
Combinations of list of lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combinations of list of lists
#1
I have a list of 25 lists of integers:
L = [[1,2,3], [4,5,6], [7,8],[9], …]


I want to create all possible combinations using all the integers from each list:

answer = [[1,4,7,9,...], [2,4,7,9,...], [3,4,7,9,...], [1,5,7,9,...], [2,5,7,9], …]


I tried:
for a in L[0]:
    for b in L[1]:
        for c in L[2]:
            ...
                answer = [a,b,c,d, …]
This works until the line length in IDLE exceeds about 90 characters, then the program refuses to run. Is there any way of using iterators, or any other solution?
Reply
#2
check itertools.product
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks Buran.

I'm new to itertools, but I'll have a look tomorrow.

Danny

I've just found a thread from heiner55 which solves my problem in 1 line:

https://python-forum.io/Thread-How-to-fi...t-of-lists

Many thanks for all the help from the forum.

Danny
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Finding combinations of list of items (30 or so) LynnS 1 836 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,023 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,014 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,561 Oct-01-2022, 07:15 PM
Last Post: Skaperen
  How to efficiently average same entries of lists in a list xquad 5 2,069 Dec-17-2021, 04:44 PM
Last Post: xquad
  sorting a list of lists by an element leapcfm 3 1,805 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  behavior list of lists roym 5 2,033 Jul-04-2021, 04:43 PM
Last Post: roym
  How can I find all combinations with a regular expression? AlekseyPython 0 1,636 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  List of lists - merge sublists with common elements medatib531 1 3,353 May-09-2021, 07:49 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020