# Difference between 'for items in list' and 'for items in range(len(list))'

#1

Question about usage of two types of for loops..

Define a function called product that takes a list of integers as input and returns the product of all of the elements in the list.

For example: product([4, 5, 5]) should return 100 (because 4 * 5 * 5 is 100).

my solution:

`def product(lst): result = lst[0] for item in range(1,len(lst)): result *= lst[item] return result`

and that worked fine. BUT, while trying to solve this, i tried some things and was getting some errors, and after some google-ing found this: http://stackoverflow.com/questions/28566577/whats-the-difference-for-item-in-list-and-for-i-in-rangelenlist

this is what bugs me:

`>>> n = [1, 2, 3]

for item in n:
... item *= 2
...
print n
[1, 2, 3]`

but when it's used for item in range:

n = [1, 2, 3]
for i in range(len(n)):
... n[i] *= 2
...

n
[2, 4, 6]

is it that item in this case is just local variable and it just returns the value of item in the list and puts it in that local variable 'item'. so when we change 'item' we are not actually changing anything in list, as 'item' variable is just representation of the value under some index in the list?

is that a main difference between those two types of for loops, and can we create some conclusion - if there is some operation to be done on an item from a list, we have to use 'for loop with range', and if we just want to quickly access value under some item and print it out or return it without changing it, then we can use 'for item in list', as somehow easier way..

would appreciate a lot if someone could help me answer this

thanks

#2

One is a look-up loop that reports the values as items. The other is an indexing loop that accesses the elements by index, so their value is `object[index]`.

Look-up loops, as I like to call them, cache the item value in the iterator variable. That value should be immutable if it is a key of a dictionary. As a list item, it should also be immutable. The iterator is internal. My own reasoning says we should not be able to tamper with it.

So read-only loops suggest no change in the data. When we can use them we should simply for their readability. Order of course is non-existent in the case of dictionaries, and enforced in lists, having a serial index.

The `in` operator takes it much further to the point where we don't need to synthecize a loop, the operator has one built in. given

``````>>> 'm' in ['m','o','o','s','e']
True
>>>``````

This implies that if we are certain the list is populated exclusively with discrete values (as would be a set) then `in` can track it down in an instant.

``````if something in object:
x = object.index(something)
object[x] = 'something else'``````

In so doing we do not mutate the iterator, only the element where it was first retrieved from. We have changed that item but the loop doesn't know it, and that item (element since it's a list) won't come up again.

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.