How to multiply each pair of consecutive items in a list with Python

2 Answers

0 votes
lst = [2, 3, 4, 5, 6, 7]
 
products = [a * b for a, b in zip(lst, lst[1:])]
 
print(products)
 
# 2 * 3 = 6
# 3 * 4 = 12
# 4 * 5 = 20
# 5 * 6 = 30
# 6 * 7 = 42
 
'''
run:
 
[6, 12, 20, 30, 42]
 
'''

 



answered Jan 31 by avibootz
edited Jan 31 by avibootz
0 votes
def products_pair_consecutive_items(lst):
    result = []

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


lst = [2, 3, 4, 5, 6, 7]

print(products_pair_consecutive_items(lst))


# 2 * 3 = 6
# 3 * 4 = 12
# 4 * 5 = 20
# 5 * 6 = 30
# 6 * 7 = 42
 
'''
run:
 
[6, 12, 20, 30, 42]
 
'''

 



answered Jan 31 by avibootz

Related questions

...