Ryan Ma
Ryan Ma

Reputation: 97

How to use range(len(list)) to find the product of all even-indexed integers in a list?

I'm very new to coding, and I'm doing an assignment where I have to find the product of all even-indexed integers in a huge list:

number_list = [12, 41, 10, 34, 37, 2, 3, 8, 42, 46, 46, 27, 13, 49, 41, 2, 28, 21, 37, 27, 31, 19, 46, 7, 50, 1, 46, 45, 19, 10, 14, 8, 44, 14, 10, 4, 23, 29, 46, 18, 32, 40, 32, 7, 33, 45, 26, 24, 43, 45]

The question recommends using range(len(list)), which gives me range (1,50), but I don't see how that's relevant. I managed to get the answer without using that method:

number_list = [12, 41, 10, 34, 37, 2, 3, 8, 42, 46, 46, 27, 13, 49, 41, 2, 28, 21, 37, 27, 31, 19, 46, 7, 50, 1, 46, 45, 19, 10, 14, 8, 44, 14, 10, 4, 23, 29, 46, 18, 32, 40, 32, 7, 33, 45, 26, 24, 43, 45]

result = 1
evenlist = number_list[::2]
for num in evenlist:
    result = result * num

How would range(len(list)) be useful here?

Upvotes: 3

Views: 280

Answers (5)

Kevin S
Kevin S

Reputation: 960

I would add that @fixatd's answer is correct but also caution you that the solution is not Pythonic. I realize your book/instructor wants the answer a certain way but I'd like to elaborate and show you some better alternatives for when you're not tied to a solution they want to see.

For example, here would be a more functional approach to the solution:

from operator import mul
from itertools import islice
from functools import reduce
number_list = [12, 41, 10, 34, 37, 2, 3, 8, 42, 46, 46, 27, 13, 49, 41, 2, 28, 21, 37, 27, 31, 19, 46, 7, 50, 1, 46, 45, 19, 10, 14, 8, 44, 14, 10, 4, 23, 29, 46, 18, 32, 40, 32, 7, 33, 45, 26, 24, 43, 45]
reduce(mul, islice(number_list, 0, None, 2))
218032559868925537961630414929920000

Alternatively, if you prefer less imports or less functional you can loop like a native. In python you'll often just loop over the iterable. The idiom is

for something in iterable:  

It's typically less pythonic to use len of something inside of range just to loop over something iteratively. If you do happen to need the indices for some reason then use enumerate:

for index, item in enumerate(iterable):

The solution that you provided is actually quite nice and pythonic as opposed to what the assignment is requesting. Here's your solution slightly cleaned up:

result = 1
for num in number_list[::2]:
    result *= num

>>>result
218032559868925537961630414929920000

Upvotes: 1

JoshuaCS
JoshuaCS

Reputation: 2624

Well, besides @fixatd's answer, you could also do it with reduce:

import operator
from functools import reduce

def prod(iterable):
    return reduce(operator.mul, iterable, 1)

result = prod(evenlist)

Upvotes: 0

Dude
Dude

Reputation: 345

Why you are interested in using range? Below there are two other variants as solution to your problem.

First way:

result = 1
for numbers in number_list:
    result *= numbers

Second way:

from functools import reduce
reduce(lambda a, b: a*b, number_list)

Upvotes: 0

Jae Yang
Jae Yang

Reputation: 539

What you can do is iterate over the array but you can specify the "jump" of each iteration as two instead of one using range(start,end,jump) . What you are doing when you are doing list[::2] is creating a new array and removing every other element then doing a iteration over it. Careful because it may take more resources for a larger input.

number_list = [12, 41, 10, 34, 37, 2, 3, 8, 42, 46, 46, 27, 13, 49, 41, 2, 28, 21, 37, 27, 31, 19, 46, 7, 50, 1, 46, 45, 19, 10, 14, 8, 44, 14, 10, 4, 23, 29, 46, 18, 32, 40, 32, 7, 33, 45, 26, 24, 43, 45]

prod = 1
for i in range(0, len(number_list), 2):
    prod *= list[i]

Upvotes: -1

fixatd
fixatd

Reputation: 1404

Might be something like this, where you reference it using the index of the array;

number_list = [12, 41, 10, 34, 37, 2, 3, 8, 42, 46, 46, 27, 13, 49, 41, 2, 28, 21, 37, 27, 31, 19, 46, 7, 50, 1, 46, 45, 19, 10, 14, 8, 44, 14, 10, 4, 23, 29, 46, 18, 32, 40, 32, 7, 33, 45, 26, 24, 43, 45]

result = 1
for idx in range(0, len(number_list), 2):
   result = result * number_list[idx]

Upvotes: 4

Related Questions