Python | Consecutive Pair Minimums
Last Updated :
17 Apr, 2023
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the minimum 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.
Method #1 : Using loop This is the brute force method to perform this particular task. In this, we just iterate the list till last element in skipped manner to get all the pair minimum in other list in iterative way.
Python3
# Python3 code to demonstrate working of
# Consecutive Pair Minimums
# Using loop
# initializing list
test_list = [4, 5, 8, 9, 10, 17]
# printing list
print("The original list : " + str(test_list))
# Consecutive Pair Minimums
# Using loop
res = []
for ele in range(0, len(test_list), 2):
res.append(min(test_list[ele], test_list[ele + 1]))
# Printing result
print("Pair minimum of list : " + str(res))
Output : The original list : [4, 5, 8, 9, 10, 17]
Pair minimum of list : [4, 8, 10]
Time Complexity: O(n*n) where n is the number of elements in the string list. The loop is used to perform the task and it takes O(n*n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the string list.
Method #2 : Using zip() + min() + list comprehension This task can also be performed using the combination of above functionalities. In this, we just iterate the list and the task of combining pairs is performed by zip(). Works only on Python2.
Python
# Python code to demonstrate working of
# Consecutive Pair Minimums
# zip() + list comprehension + min()
# initializing list
test_list = [4, 5, 8, 9, 10, 17]
# printing list
print("The original list : " + str(test_list))
# Consecutive Pair Minimums
# zip() + list comprehension + min()
res = [min(i, j) for i, j in zip(test_list, test_list[1:])[::2]]
# Printing result
print("Pair minimum of list : " + str(res))
Output : The original list : [4, 5, 8, 9, 10, 17]
Pair minimum of list : [4, 8, 10]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3 : Using map() + min()
# This task can be performed using combination of above functions. In this, we use the map() to perform the task of combining the elements of list and min() to perform the task of finding the minimum.
Python3
# Python3 code to demonstrate working of
# Consecutive Pair Minimums
# map() + min()
# initializing list
test_list = [4, 5, 8, 9, 10, 17]
# printing list
print("The original list : " + str(test_list))
# Consecutive Pair Minimums
# map() + min()
res = list(map(min, test_list[::2], test_list[1::2]))
# Printing result
print("Pair minimum of list : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list : [4, 5, 8, 9, 10, 17]
Pair minimum of list : [4, 8, 10]
Time complexity: o(n)
Auxiliary Space: o(n)
Method#4: Using Recursive method.
Algorithm:
- Define a function "consecutive_pair_min_recursive" that takes a list "lst" as input.
- If the length of the list "lst" is 0, return an empty list.
- If the length of the list "lst" is 1, return the list itself.
- Otherwise, find the minimum value of the first two elements in the list "lst" and store it in a new list.
- Recursively call the function "consecutive_pair_min_recursive" on the list "lst" starting from the third element (i.e., the index 2) and concatenate the result to the new list obtained in step 4.
- Return the new list obtained in step 5.
Python3
def consecutive_pair_min_recursive(lst):
if len(lst) == 0:
return []
elif len(lst) == 1:
return lst
else:
return [min(lst[0], lst[1])] + consecutive_pair_min_recursive(lst[2:])
# initializing list
test_list = [4, 5, 8, 9, 10, 17]
# printing list
print("The original list : " + str(test_list))
# Finding consecutive pair minimums using recursion
res = consecutive_pair_min_recursive(test_list)
# Printing result
print("Pair minimum of list : " + str(res))
#this code contributed by tvsk
OutputThe original list : [4, 5, 8, 9, 10, 17]
Pair minimum of list : [4, 8, 10]
Time complexity: The time complexity of the recursive method is O(n), where n is the number of elements in the input list. This is because the function visits each element in the list exactly once.
Auxiliary space: The auxiliary space complexity of the recursive method is O(n), where n is the number of elements in the input list. This is because the function creates a new list of size n to store the result, and the maximum number of function calls on the call stack is also proportional to n.
Similar Reads
Python | Consecutive Subsets Minimum
Some of the classical problems in programming domain comes from different categories and one among them is finding the minimum of subsets. This particular problem is also common when we need to accumulate the minimum and store consecutive group minimum. Letâs try different approaches to this problem
4 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 - 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 | Minimum key equal pairs
Sometimes, while working with Python, we can have a problem in which we need to get all the records. This data can have similar values and we need to find minimum keyâed value pair. This kind of problem can occur while working with data. Letâs discuss certain ways in which this task can be done. Met
3 min read
Python - Maximum of Consecutive Pair in integer list
Sometimes, while working with Python list, one can have a problem in which one needs to find perform the maximization 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 so
5 min read
Python | Minimum Sum of Consecutive Characters
Sometimes, we might have a problem in which we require to get the minimum sum of 2 numbers from list but with the constraint of having the numbers in successions. This type of problem can occur while competitive programming. Letâs discuss certain ways in which this problem can be solved. Method #1:
5 min read
Python - Closest Sum Pair in List
Sometimes, we desire to get the elements that sum to a particular element. But in cases we are not able to find that, our aim changes to be one to find the closest one. This can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using diction
4 min read
Python | Consecutive Maximum Occurrence in list
Sometimes, while working with Python lists or in competitive programming setup, we can come across a subproblem in which we need to get an element which has the maximum consecutive occurrence. The knowledge of the solution of it can be of great help and can be employed whenever required. Let's discu
5 min read
Python - Minimum Product Pair in List
Sometimes, we need to find the specific problem of getting the pair that yields the minimum product, this can be solved by sorting and getting the first and second elements of the list. But in some case, we donât with to change the ordering of list and perform some operation in the similar list with
3 min read
Python - Test Consecutive Element Matrix
Given a Matrix, test if it is made of consecutive elements. Input : test_list = [[4, 5, 6], [7], [8, 9, 10], [11, 12]] Output : True Explanation : Elements in Matrix range from 4 to 12. Input : test_list = [[4, 5, 6], [7], [8, 18, 10], [11, 12]] Output : False Explanation : Elements not consecutive
6 min read