Really confused about this list exercise I was working on

Regarding Python 3, I am on the loop course and trying to solve the exercises (which are very difficult for me)

I am trying to understand, using a for loop, what is the difference between

for x in lst

and

for x in range(len(lst))

This is specific to exercise 10 of the loop problems called “same values”.

in the case of the former, the loop would iterate through all items in list “lst” correct?
What about the latter? It would iterate through all items of “lst” within the range of the total amount of items in “lst”? Am i understanding correctly?

Thank you in advance for your assistance

Given a list,

s = [ 'one', 'two', 'three', 'four', 'five', 'six', 'seven' ]

Now consider,

for item in s:                # loop signature
    print (item, end=', ')    # loop body
print ()

Output

one, two, three, four, five, six, seven,

Notice how we access the items directly using the above loop signature?

Now let’s try,

list_index = range(len(s))     # range object

for i in list_index:
    print ((i, s[i]), end=", "
print ()

Output

(0, 'one'), (1, 'two'), (2, 'three'), (3, 'four'), (4, 'five'), (5, 'six'), (6, 'seven'),

Above we access the items by their index.

Direct access is read-only, meaning we cannot modify elements of the list, only access them for reading (known as polling).

Access by index is both read and write, meaning we can modify the value at a given index. Notice we first needed to create the index from a range of the same length as the list?

4 Likes

Yes you are correct that former one iterates through all items in list “lst”. But wrong about the other one.
len(lst) returns the length of the argument passed in (in this case length of list).

For ex,

lst = ["hi", "hello", "new", "fourth"]
print(len(lst))
# prints out 4 as it is clear that length (or number of elements) in list "lst" is 4

Now, range() function generates an iterable object. Confused? Let’s make this more simpler

We know everything is object in python. So any list is also a object. Range() generates a list object.
It takes 1 necessary and 2 optional arguments. All three needs to be integers.

So If we do like this

lst = range(5)
print(lst)
# Will print range(0, 5). This is surely not a list. But let's understand this first

range(0, 5) means every number between 0 (including) and 5 (excluding). A by default step size (difference between each number) of 1. So it would be like 0, 1, 2, 3, 4. How to get a list from this? Simple,

lst = list(range(5))
print(lst)
# gives [0, 1, 2, 3, 4]

I hope by now range() would be clear. (Why did I explain range()? because it’s important in your problem)

Using range() with list() around it generates a object which can be iterated upon, meaning we can loop on it.

In your case, len(lst) will give a integer (length or num of elements in lst) and using range() on len(lst) will give a iterable object which is a list of numbers from 0 to len(lst) , not including len(lst), with step size 1.

Some example code:

lst = [67, 84, 36, 37, 76, 37]

for x in lst:
    print(x)

""" prints: 
67
84
36
37
76
37
"""

for x in range(len(lst)):
    print(x)

""" prints:
0
1
2
3
4
5
"""

I hope you understood.
Happy Coding :slightly_smiling_face:.

Yes, a range object is iterable, else how could we use it in a for statement?

Not in Python 3, it doesn’t. It generates a range object. It is not a list, but it is iterable, even without being cast to one.

We do not need to cast the range object to a list. It is iterable already, as stated above. A list is an expensive object to generate and store. A range is a simple object that is always one size no matter how large the expanse between start and end.

Suggest you do a little reading, @armaan_barak, before posting TL;DR that is misinformed.

2 Likes

I just mentioned it in the example second code here,

That part was for explaning that how we get a list from range().

I’m really sorry if it sound confusing for you but I thought that it was better if I explained everything in detail.

1 Like