FAQ: Code Challenge: Loops - Reversed List

What does the range look like when the list is empty?

Range is 0 when the list is empty.

But the solution code also used range?

The loop will not run if the list is empty, so when an empty list is passed into the function, there is no return outside of the loop so Python returns None.

Hello

I’m trying to better understand these challenges, because I can see that I’m having a lot of work with the way lists work. Specially the index part of the list, and the range-len thing.

That being said, I’m trying my best with this exercise but i can’t seem to get it.

Could you guys give me a hand please.

Here is my code:

A list can best be described as an ordered sequence. By order we mean that insertion order is preserved. Mathematically, a sequence of numbers is a vector since they either get bigger from left to right, or bigger from right to left. There is a common difference between terms.

['one', 'two', 'three', 'four', 'five']

Above is a list, but it is hard to reconcile it as a mathematical sequence. If we enumerate it, though, the sequential aspect jumps right off the page.

>>> list(enumerate(['one', 'two', 'three', 'four', 'five']))
[(0, 'one'), (1, 'two'), (2, 'three'), (3, 'four'), (4, 'five')]
>>> 

The numbers are the equivalent of indices of the base list. Note that it is ordered and sequential from 0 thru to 4. Note also that the indices of any iterable always start at zero. Thus, the last index is 1 less than the length of the list.

When we use a range to control iteration of a list, we are simply generating a sequence that starts with zero and increases by 1 on each subsequent term. The indices are not part of the list, physically, but the since the order of the list elements is always sequential, we can access the elements of the list by their unique index (position in the list).


Let’s see what a basic range looks like once we cast it to a list. (A range object is not a list.)

>>> a = ['one', 'two', 'three', 'four', 'five']
>>> b = list(range(len(a)))
>>> b
[0, 1, 2, 3, 4]
>>> 

Now we can iterate over b to access each of the elementa of a. Their lengths are the same since we generated the range to match the length of the list.

>>> for i in b:
    print (a[i])

    
one
two
three
four
five
>>> 

There are infinite configurations for a range. The start value can be any integer. The end value can be any integer. The stride can be any integer. However, when relating one to the indices of a list, it always begins with 0 and increases to the length of the list. The highest value is excluded from the range.

Hi all, I am trying to solve this challenge and I am aware that the solution solves this a different way - which is fine.

I did up another way and while I understand the solution method, I want to know why my code does NOT work! Instead of returning ‘False’ or ‘True’, it spits out ‘None’ ‘None’ every time.

Is there an error in my code that I am missing or is there something that needs to be added?..

MyCode

NoneNone

Thank you in advance.

In line 5 the stride should be -1 and the stop should also be -1 so that zero is reached. The start should be len(lst2) - 1, or simply, -1 which is the same element.

Consider also that in order to complete iteration there should not be a return in the loop, and no else clause. If we test for non-equality, then it will be possible to return False in the loop, but that is all. Have the function return True when the loop finishes iterating.

Thank you for your quick and concise response Roy. I enjoy reading your responses all over the forum.

1 Like