Python - Cross Pattern Pairs in List
Last Updated :
16 May, 2023
Given two lists, pair them diagonally, in cross pattern [works for even and equi-length lists].
Input : test_list1 = [4, 5, 6, 2], test_list2 = [9, 1, 4, 7]
Output : [(4, 1), (5, 9), (6, 7), (2, 4)]
Explanation : Crosslinked diagonally, 4->1, 5->9.
Input : test_list1 = [4, 5], test_list2 = [9, 1]
Output : [(4, 1), (5, 9)]
Explanation : Crosslinked diagonally, 4->1, 5->9.
Method #1: Using loop
In this, we iterate through list and test for even or odd index, in case of even index, we pair with next element of other list, or else we pair with previous element of other list. This way cross pattern tuples are formed.
Python3
# Python3 code to demonstrate working of
# Cross Pattern Pairs in List
# Using loop
# function to generate cross pattern pairs
def crossPair(test_list1, test_list2):
# lengths of both lists should be equal
if len(test_list1) != len(test_list2):
return -1
res = []
for idx in range(len(test_list1)):
# checking for conditions
if idx % 2 == 0:
res.append((test_list1[idx], test_list2[idx + 1]))
else:
res.append((test_list1[idx], test_list2[idx - 1]))
return res
# initializing lists
input_list1 = [4, 5, 6, 2, 8, 9]
input_list2 = [9, 1, 4, 7, 9, 2]
# printing original lists
print("The original list 1 is : " + str(input_list1))
print("The original list 2 is : " + str(input_list2))
# printing result
print("Paired List elements : ", crossPair(input_list1, input_list2))
Output:
The original list 1 is : [4, 5, 6, 2, 8, 9] The original list 2 is : [9, 1, 4, 7, 9, 2] Paired List elements : [(4, 1), (5, 9), (6, 7), (2, 4), (8, 2), (9, 9)]
Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(n), where n is the length of the input lists.
Method #2: Using list comprehension
Uses similar approach as above method, difference being list comprehension is used as a single linear alternative to solve this problem.
Python3
# Python3 code to demonstrate working of
# Cross Pattern Pairs in List
# Using list comprehension
# function to generate cross pattern pairs
def crossPair(test_list1, test_list2):
# lengths of both lists should be equal
if len(test_list1) != len(test_list2):
return -1
# list comprehension used as one liner alternative
res = [(test_list1[idx], test_list2[idx + 1]) if idx % 2 ==
0 else (test_list1[idx], test_list2[idx - 1]) for idx in range(len(test_list1))]
return res
# initializing lists
input_list1 = [4, 5, 6, 2, 8, 9]
input_list2 = [9, 1, 4, 7, 9, 2]
# printing original lists
print("The original list 1 is : " + str(input_list1))
print("The original list 2 is : " + str(input_list2))
# printing result
print("Paired List elements : ", crossPair(input_list1, input_list2))
Output:
The original list 1 is : [4, 5, 6, 2, 8, 9] The original list 2 is : [9, 1, 4, 7, 9, 2] Paired List elements : [(4, 1), (5, 9), (6, 7), (2, 4), (8, 2), (9, 9)]
Method#3: Using Recursive method.
Python3
# Python3 code to demonstrate working of
# Cross Pattern Pairs in List
# Using recursion function
def crossPair(test_list1, test_list2, idx=0, res=[]):
if idx == len(test_list1):
return res
if idx % 2 == 0:
res.append((test_list1[idx], test_list2[idx + 1]))
else:
res.append((test_list1[idx], test_list2[idx - 1]))
return crossPair(test_list1, test_list2, idx + 1, res)
# initializing lists
input_list1 = [4, 5, 6, 2, 8, 9]
input_list2 = [9, 1, 4, 7, 9, 2]
# printing original lists
print("The original list 1 is : " + str(input_list1))
print("The original list 2 is : " + str(input_list2))
# printing result
print("Paired List elements : ", crossPair(input_list1, input_list2))
#this code contributed by tvsk
OutputThe original list 1 is : [4, 5, 6, 2, 8, 9]
The original list 2 is : [9, 1, 4, 7, 9, 2]
Paired List elements : [(4, 1), (5, 9), (6, 7), (2, 4), (8, 2), (9, 9)]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using numpy():
Algorithm:
- Define a function called crossPair that takes in two lists as arguments.
- Create a list comprehension that iterates through the length of the first list using numpy's arange function
- For each element in the first list, check if its index is even or odd using the modulo operator.
- If the index is even, append a tuple of the current element from the first list and the element at the next index in the second list to a new list.
- If the index is odd, append a tuple of the current element from the first list and the element at the previous index in the second list to the new list.
- Return the new list of paired elements
- Initialize two input lists and print them.
- Call the crossPair function with the two input lists as arguments and print the result.
Python3
import numpy as np
def crossPair(test_list1, test_list2):
return [(test_list1[i], test_list2[i-1] if i%2 else test_list2[i+1]) for i in np.arange(len(test_list1))]
# initializing lists
input_list1 = [4, 5, 6, 2, 8, 9]
input_list2 = [9, 1, 4, 7, 9, 2]
# printing original lists
print("The original list 1 is : " + str(input_list1))
print("The original list 2 is : " + str(input_list2))
# printing result
print("Paired List elements : ", crossPair(input_list1, input_list2))
#This code is contributed by Jyothi pinjala
Output:
The original list 1 is : [4, 5, 6, 2, 8, 9]
The original list 2 is : [9, 1, 4, 7, 9, 2]
Paired List elements : [(4, 1), (5, 9), (6, 7), (2, 4), (8, 2), (9, 9)]
Time Complexity: O(n), where n is the length of the input list. This is because the algorithm iterates through each element of the input list once.
Space Complexity: O(n), where n is the length of the input list. This is because the algorithm creates a new list to store the paired elements, which will be the same length as the input list.
Method 5: Using the map function and a lambda function
Step-by-step approach:
- Use the built-in map function to apply a lambda function to the two input lists simultaneously.
- The lambda function should take two arguments, one from each list, and return a tuple with the elements in the cross pattern.
- Use the list function to convert the map object into a list of tuples.
- Return the list of paired elements.
Python3
def crossPair(test_list1, test_list2):
pairs = list(map(lambda x, y: (x, test_list2[test_list1.index(x)+1]) if test_list1.index(x) % 2 == 0 else (x, test_list2[test_list1.index(x)-1]), test_list1, test_list2))
return pairs
# initializing lists
input_list1 = [4, 5, 6, 2, 8, 9]
input_list2 = [9, 1, 4, 7, 9, 2]
# printing original lists
print("The original list 1 is : " + str(input_list1))
print("The original list 2 is : " + str(input_list2))
# printing result
print("Paired List elements : ", crossPair(input_list1, input_list2))
OutputThe original list 1 is : [4, 5, 6, 2, 8, 9]
The original list 2 is : [9, 1, 4, 7, 9, 2]
Paired List elements : [(4, 1), (5, 9), (6, 7), (2, 4), (8, 2), (9, 9)]
Time complexity: O(n), where n is the length of the input lists, as it requires iterating through both lists.
Auxiliary space: O(n) to store the list of paired elements.
Similar Reads
Python - Cross Pairing in Tuple List
Given 2 tuples, perform cross pairing of corresponding tuples, convert to single tuple if 1st element of both tuple matches. Input : test_list1 = [(1, 7), (6, 7), (8, 100), (4, 21)], test_list2 = [(1, 3), (2, 1), (9, 7), (2, 17)] Output : [(7, 3)] Explanation : 1 occurs as tuple element at pos. 1 in
5 min read
All Possible Pairs in List - Python
We are given a list and our task is to generate all possible pairs from the list. Each pair consists of two distinct elements from the list. For example: a = [1, 2, 3] then the output will be [(1,2), (1,3), (2,3)].Using combinations() from itertoolscombinations() function from the itertools module g
2 min read
Python - Product of consecutive pairs in list
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the product of list in pair form. This is useful as a subproblem solution of bigger problem in web development and day-day programming. Letâs discuss certain ways in which this problem can be solved.
7 min read
Python - Iterative Pair Pattern
Sometimes, while working with Python, we can have problem in which we need to perform the pattern construction or iterative pair string in which second element keeps increasing. This kind of problem can have application in day-day programming and school programming. Lets discuss certain ways in whic
3 min read
Python | Unique pairs in list
Sometimes, while working with python list, we can have a binary matrix ( Nested list having 2 elements ). And we can have a problem in which we need to find the uniqueness of a pair. A pair is unique irrespective of order, it doesn't appear again in list. Let's discuss certain way in which this task
6 min read
Python | Consecutive elements pairing in list
This process involves creating pairs of elements that appear next to each other, which can be invaluable for various applications such as data analysis, pattern recognition and algorithm development. We can achieve this using different methods in Python, such as using simple loops, list slicing, zip
3 min read
Python - Interlist Perfect Square Pairs
Sometimes, while working in Mathematics domain, we can have a problem in which we need to check if product of elements from different lists can lead to perfect square or not. This can have application in many domains including mathematics and web development. Lets discuss certain ways in which this
5 min read
Print odd numbers in a List - Python
We are given a list and our task is to print all the odd numbers from it. This can be done using different methods like a simple loop, list comprehension, or the filter() function. For example, if the input is [1, 2, 3, 4, 5], the output will be [1, 3, 5].Using LoopThe most basic way to print odd nu
2 min read
Python - Multiply all cross list element pairs
Sometimes, while working with Python lists, we can have a problem in which we need to perform the multiplication of each element of list with another list. This can have applications in both web development and day-day programming. Let's discuss certain ways in which this task can be performed. Meth
8 min read
Python - Total equal pairs in List
Given a list, the task is to write a python program to compute total equal digit pairs, i.e extract the number of all elements with can be dual paired with similar elements present in the list. Input : test_list = [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3] Output : 4 Explanation : 4, 2 and 5 have 3 occ
7 min read