How to make pairs in python

Create set of pairs [even,odd] combination

>>> a = { [i,j] for i in range[0,10,2] for j in range[1,10,2]}  
>>> a
{[4, 7], [6, 9], [0, 7], [2, 1], [8, 9], [0, 3], [2, 5], [8, 5], [4, 9], [6, 7], [2, 9], [8, 1], [6, 3], [4, 1], [4, 5], [0, 5], [2, 3], [8, 7], [6, 5], [0, 1], [2, 7], [8, 3], [6, 1], [4, 3], [0, 9]}

def combinations[lista, listb]:
    return { [i,j] for i in lista for j in listb }

>>> combinations[[1,3,5,6],[11,21,133,134,443]]
{[1, 21], [5, 133], [5, 11], [5, 134], [6, 11], [6, 134], [1, 443], [3, 11], [6, 21], [3, 21], [1, 133], [1, 134], [5, 21], [3, 134], [5, 443], [6, 443], [1, 11], [3, 443], [6, 133], [3, 133]}

Sometimes, while working with Python list, we can have a problem in which we need to extract all the possible pairs that can be performed from integers from list. This kind of problem can occur in many domains such as day-day programming and web development. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [1, 7, 4]
Output : [[1, 7], [1, 4], [7, 4]]

Input : test_list = [7, 4]
Output : [[7, 4]]

Method #1 : Using list comprehension + enumerate[]
This is one of the ways in which this task can be performed. In this, we perform the task of pairing using nested loops in list comprehension recipe, and enumerate[] is used to check with the next indices while iteration.

test_list = [1, 7, 4, 3]

print["The original list : " + str[test_list]]

res = [[a, b] for idx, a in enumerate[test_list] for b in test_list[idx + 1:]]

print["All possible pairs : " + str[res]]

Output :

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]

Method #2 : Using combinations[]
This is one of the ways in which this task can be performed. In this, we just using inbuild function for pairing and sending 2 as value for making pairs of size 2.

from itertools import combinations

test_list = [1, 7, 4, 3]

print["The original list : " + str[test_list]]

res = list[combinations[test_list, 2]]

print["All possible pairs : " + str[res]]

Output :

The original list : [1, 7, 4, 3]
All possible pairs : [[1, 7], [1, 4], [1, 3], [7, 4], [7, 3], [4, 3]]


List iteration is common in Python programming, but sometimes one requires to print the elements in consecutive pairs. This particular problem is quite common and having a solution to it always turns out to be handy. Let’s discuss certain ways in which this problem can be solved. 

Pair iteration in a list using list comprehension 

List comprehension can be used to print the pairs by accessing the current and next elements in the list and then printing the same. Care has to be taken while pairing the last element with the first one to form a cyclic pair. 

Python3

from itertools import compress

test_list = [0, 1, 2, 3, 4, 5]

print ["The original list is : " + str[test_list]]

res = [[[i], [i + 1] % len[test_list]]

        for i in range[len[test_list]]]

print ["The pair list is : " + str[res]]

Output:

The original list is : [0, 1, 2, 3, 4, 5]
The pair list is : [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0]]

Pair iteration in a list using zip[] + list slicing 

The zip function can be used to extract pairs over the list slicing can be used to successively pair the current element with the next one for efficient pairing. 

Python3

from itertools import compress

test_list = [0, 1, 2, 3, 4, 5]

print ["The original list is : " + str[test_list]]

res = list[zip[test_list, test_list[1:] + test_list[:1]]]

print ["The pair list is : " + str[res]]

Output:

The original list is : [0, 1, 2, 3, 4, 5]
The pair list is : [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 0]]

How do you make a pair list in Python?

In this article, we are going to learn how to make pairs from two lists such that no similar elements make a pair..
Initialize the lists with elements..
Iterate over the lists and append the pair into a list if the corresponding elements from the lists are not same..
Print the result..

What is a pair in Python?

Pairing functions take two integers and give you one integer in return. What makes a pairing function special is that it is invertable; You can reliably depair the same integer value back into it's two original values in the original order.

How do you add a pair of values to a list in Python?

Syntax. Python dictionary update[] function accepts an iterable sequence of key-value pairs that can be a single key-value pair or list of tuples or another dictionary. For each entry in sequence, it will add the given key-value pair in the Dictionary, and if the key already exists then, it will update its value.

How do you make a pair with two lists?

how to pair up two lists in python.
x = [1, 2, 3].
y = [4, 5, 6].
l = list[zip[x, y]].
print[l].
>>> [[1, 4], [2, 5], [3, 6]].

Chủ Đề