Python Forum
Do something with all possible combinations of a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do something with all possible combinations of a list
#4
(Sep-11-2019, 07:20 AM)3Pinter Wrote: It does leave out the 'themselves'-ones but not the duplicate matches.

In my case [1,6] will return the same result (if doing a function) as [6,1]

I think I found a way:
listA = [1,2,3,4,5,6]
listB = listA[:]
output = []
for a in listA: 
    for b in listB:
        if b != a and b > a:
            # function here
            output.append([a,b])
print(output)
and that little modification produces the following output:
Output:
[[1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 3], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6], [4, 5], [4, 6], [5, 6]]
All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Messages In This Thread
RE: Do something with all possible combinations of a list - by newbieAuggie2019 - Sep-11-2019, 07:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding combinations of list of items (30 or so) LynnS 1 938 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  How can I find all combinations with a regular expression? AlekseyPython 0 1,725 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  All possible combinations CODEP 2 1,932 Dec-01-2020, 06:10 PM
Last Post: deanhystad
  Triplet Combinations of All Values quest 2 2,070 Nov-05-2020, 09:22 AM
Last Post: quest
  All possible combinations of multiplications Shreya10o 0 1,715 May-23-2020, 07:45 AM
Last Post: Shreya10o
  counting items in a list of number combinations Dixon 2 2,141 Feb-19-2020, 07:06 PM
Last Post: Dixon
  Python program that list all possible football outcome combinations lukorir 5 9,111 Oct-17-2019, 04:05 AM
Last Post: steve_shambles
  list of string combinations Skaperen 8 3,473 May-22-2019, 01:18 PM
Last Post: Skaperen
  Python to iterate a number of possible combinations teflon 4 4,046 Apr-24-2019, 03:00 AM
Last Post: scidam
  Distances between all combinations in datatset amyd 6 4,003 Dec-13-2018, 01:23 PM
Last Post: amyd

Forum Jump:

User Panel Messages

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