Finding the Product of Consecutive Pairs in a List

Finding the product of consecutive pairs in a list means multiplying each element with its next neighbor. For example, given [1, 2, 3, 4, 5], we create pairs (1,2), (2,3), (3,4), (4,5) and multiply each pair to get [2, 6, 12, 20].

Understanding the Problem

Given a list of numbers, we need to ?

  • Form consecutive pairs from adjacent elements
  • Multiply each pair to get the product
  • Return a new list containing all products

For the list [1, 2, 3, 4, 5] ?

  • Pair (1,2) ? Product: 1 × 2 = 2
  • Pair (2,3) ? Product: 2 × 3 = 6
  • Pair (3,4) ? Product: 3 × 4 = 12
  • Pair (4,5) ? Product: 4 × 5 = 20

Result: [2, 6, 12, 20]

Method 1: Using a Loop

Iterate through the list and multiply each element with its next neighbor ?

def consecutive_products(numbers):
    result = []
    for i in range(len(numbers) - 1):
        product = numbers[i] * numbers[i + 1]
        result.append(product)
    return result

# Example usage
numbers = [1, 2, 3, 4, 5]
products = consecutive_products(numbers)
print("Original list:", numbers)
print("Consecutive products:", products)
Original list: [1, 2, 3, 4, 5]
Consecutive products: [2, 6, 12, 20]

Method 2: Using List Comprehension

A more concise approach using list comprehension ?

def consecutive_products_compact(numbers):
    return [numbers[i] * numbers[i + 1] for i in range(len(numbers) - 1)]

# Example usage
numbers = [2, 4, 6, 8, 10]
products = consecutive_products_compact(numbers)
print("Original list:", numbers)
print("Consecutive products:", products)
Original list: [2, 4, 6, 8, 10]
Consecutive products: [8, 24, 48, 80]

Method 3: Using zip()

Using zip() to pair consecutive elements ?

def consecutive_products_zip(numbers):
    return [a * b for a, b in zip(numbers, numbers[1:])]

# Example usage
numbers = [3, 5, 7, 9]
products = consecutive_products_zip(numbers)
print("Original list:", numbers)
print("Consecutive products:", products)
Original list: [3, 5, 7, 9]
Consecutive products: [15, 35, 63]

Handling Edge Cases

Consider empty lists or lists with single elements ?

def safe_consecutive_products(numbers):
    if len(numbers) < 2:
        return []
    return [numbers[i] * numbers[i + 1] for i in range(len(numbers) - 1)]

# Test edge cases
print("Empty list:", safe_consecutive_products([]))
print("Single element:", safe_consecutive_products([5]))
print("Two elements:", safe_consecutive_products([3, 7]))
Empty list: []
Single element: []
Two elements: [21]

Comparison

Method Readability Performance Best For
For Loop High Good Beginners, clear logic
List Comprehension Medium Best Concise code
zip() Function Medium Good Functional programming

Conclusion

Finding products of consecutive pairs is straightforward using loops or list comprehension. The list comprehension approach is most efficient, while the loop method offers better readability for beginners.

Updated on: 2026-03-27T10:19:00+05:30

420 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements