How do you get the combination of two numbers in python?

Let's say I have a list of four values. I want to find all combinations of two of the values. For example, I would like to get an output like:

[[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]

As you can see, I do not want repetitions, for example [0, 1] and [1, 0]

This needs to be able to be used with larger numbers, not just 4, and I will have to iterate through all of the combos

I am using Python 3 and Windows, and this would ideally be an inbuilt function, a simple bit of list comprehension code, or something I can import. I have tried making this with range, but I do not know how to exclude the numbers that I have already done from it.

The combination is a mathematical technique which calculates the number of possible arrangements in a collection of items or list. In combination order of selection doesn’t matter. The unique combination of two lists in Python can be formed by pairing each element of the first list with the elements of the second list. 

Example:

List_1 = ["a","b"]
List_2 = [1,2]
Unique_combination = [[['a',1],['b',2]],[['a',2],['b',1]]] 

Method 1 : Using permutation[] of itertools package and zip[] function.

Approach :

  • Import itertools package and initialize list_1 and list_2.
  • Create an empty list of ‘unique_combinations’ to store the resulting combinations so obtained.
  • Call itertools.permutations[ ] which will return permutations of list_1 with length of list_2. Generally, the length of the shorter list is taken and if both lists are equal, use either.
  • For loop is used and zip[] function is called to pair each permutation and shorter list element into the combination.
  • Then each combination is converted into a list and append to the combination list.

Below is the implementation.

Python3

import itertools

from itertools import permutations

list_1 = ["a", "b", "c","d"]

list_2 = [1,4,9]

unique_combinations = []

permut = itertools.permutations[list_1, len[list_2]]

for comb in permut:

    zipped = zip[comb, list_2]

    unique_combinations.append[list[zipped]]

print[unique_combinations]

Output :

[[[‘a’, 1], [‘b’, 4], [‘c’, 9]], [[‘a’, 1], [‘b’, 4], [‘d’, 9]], [[‘a’, 1], [‘c’, 4], [‘b’, 9]], [[‘a’, 1], [‘c’, 4], [‘d’, 9]], [[‘a’, 1], [‘d’, 4], [‘b’, 9]], [[‘a’, 1], [‘d’, 4], [‘c’, 9]], [[‘b’, 1], [‘a’, 4], [‘c’, 9]], [[‘b’, 1], [‘a’, 4], [‘d’, 9]], [[‘b’, 1], [‘c’, 4], [‘a’, 9]], [[‘b’, 1], [‘c’, 4], [‘d’, 9]], [[‘b’, 1], [‘d’, 4], [‘a’, 9]], [[‘b’, 1], [‘d’, 4], [‘c’, 9]], [[‘c’, 1], [‘a’, 4], [‘b’, 9]], [[‘c’, 1], [‘a’, 4], [‘d’, 9]], [[‘c’, 1], [‘b’, 4], [‘a’, 9]], [[‘c’, 1], [‘b’, 4], [‘d’, 9]], [[‘c’, 1], [‘d’, 4], [‘a’, 9]], [[‘c’, 1], [‘d’, 4], [‘b’, 9]], [[‘d’, 1], [‘a’, 4], [‘b’, 9]], [[‘d’, 1], [‘a’, 4], [‘c’, 9]], [[‘d’, 1], [‘b’, 4], [‘a’, 9]], [[‘d’, 1], [‘b’, 4], [‘c’, 9]], [[‘d’, 1], [‘c’, 4], [‘a’, 9]], [[‘d’, 1], [‘c’, 4], [‘b’, 9]]] 
 

Method 2 : Using product[] of itertools package and zip[] function.

Approach :

  • Import itertools package and initialize list_1 and list_2.
  • Create an empty list of ‘unique_combinations’ to store the resulting combinations so obtained.
  • product[] is called to find all possible combinations of elements.
  • And zip[] is used to pair up all these combinations, converting each element into a list and append them to the desired combination list.

Below is the implementation.

Python3

import itertools

from itertools import product

list_1 = ["b","c","d"]

list_2 = [1,4,9]

unique_combinations = []

unique_combinations = list[list[zip[list_1, element]]

                           for element in product[list_2, repeat = len[list_1]]]

print[unique_combinations]

Output :

[[[‘b’, 1], [‘c’, 1], [‘d’, 1]], [[‘b’, 1], [‘c’, 1], [‘d’, 4]], [[‘b’, 1], [‘c’, 1], [‘d’, 9]], [[‘b’, 1], [‘c’, 4], [‘d’, 1]], [[‘b’, 1], [‘c’, 4], [‘d’, 4]], [[‘b’, 1], [‘c’, 4], [‘d’, 9]], [[‘b’, 1], [‘c’, 9], [‘d’, 1]], [[‘b’, 1], [‘c’, 9], [‘d’, 4]], [[‘b’, 1], [‘c’, 9], [‘d’, 9]], [[‘b’, 4], [‘c’, 1], [‘d’, 1]], [[‘b’, 4], [‘c’, 1], [‘d’, 4]], [[‘b’, 4], [‘c’, 1], [‘d’, 9]], [[‘b’, 4], [‘c’, 4], [‘d’, 1]], [[‘b’, 4], [‘c’, 4], [‘d’, 4]], [[‘b’, 4], [‘c’, 4], [‘d’, 9]], [[‘b’, 4], [‘c’, 9], [‘d’, 1]], [[‘b’, 4], [‘c’, 9], [‘d’, 4]], [[‘b’, 4], [‘c’, 9], [‘d’, 9]], [[‘b’, 9], [‘c’, 1], [‘d’, 1]], [[‘b’, 9], [‘c’, 1], [‘d’, 4]], [[‘b’, 9], [‘c’, 1], [‘d’, 9]], [[‘b’, 9], [‘c’, 4], [‘d’, 1]], [[‘b’, 9], [‘c’, 4], [‘d’, 4]], [[‘b’, 9], [‘c’, 4], [‘d’, 9]], [[‘b’, 9], [‘c’, 9], [‘d’, 1]], [[‘b’, 9], [‘c’, 9], [‘d’, 4]], [[‘b’, 9], [‘c’, 9], [‘d’, 9]]] 
 


How do you find the number of combinations in Python?

To calculate the combinations of a dictionary in Python, use the itertools. combinations[] method. The combinations[] method takes a dictionary as an argument and returns all the possible combinations of the dictionary elements. Let's define a dictionary and perform the combinations on the item of the tuple.

How do you make a two list combination in Python?

The unique combination of two lists in Python can be formed by pairing each element of the first list with the elements of the second list. Method 1 : Using permutation[] of itertools package and zip[] function. Approach : Import itertools package and initialize list_1 and list_2.

How do you find the combination of numbers that equal a sum in Python?

First, we take an empty list 'res' and start a loop and traverse each element of the given list of integers. In each iteration, pop the element, store it in 'num', find remaining difference for sum K, and check if the difference exists in the given list or not.

How does combinations work in Python?

combinations[] do ? It returns r length subsequences of elements from the input iterable. Combinations are emitted in lexicographic sort order. So, if the input iterable is sorted, the combination tuples will be produced in sorted order.

Chủ Đề