How can I iterate over a list backward?

Thanks for keeping me focused. You’re right. It’s not overnight - this is a marathon, not a sprint.

Can you guide me to the places where I can practice these things? Im redoing the lessons but because I’ve done them already, I’m not sure if I’m learning or just memorizing them.

can’t see why my code doesnt get accepted… sure it’s ugly as ■■■■ but it seems to me it’s doing its job alright

def reversed_list(lst1, lst2):
    for element in lst1:
      if element == lst2[-1]:
          del lst2[-1]
          if lst2 == []:
              return True
      else:
          return False

you missed an edge case, the exercise tells you what edge case you missed:
reversed_list([], []) should have returned True , and it returned None

and if you add this call to your code:

def reversed_list(lst1, lst2):
    for element in lst1:
      if element == lst2[-1]:
          del lst2[-1]
          if lst2 == []:
              return True
      else:
          return False

print(reversed_list([], []))

you can see None. Which can mean two things, in this case it means there was no return value/absence of return value

I thought it was a rather interesting exercise. My first instinct was to try and count backwards through the list to create a new, reversed list, then compare it to the first list using list comprehension. I will share it here in case anyone finds it helpful:

backdex = [(-1 * i) for i in range(1, len(lst2) + 1)]
lst2_bw = [lst2[i] for i in backdex]
if lst1 == lst2_bw:

I then reviewed the solution provided, which I thought was a novel approach too. I tried to avoid using shortcuts with built-in functions and stick to what they have taught us so far.

Thank you for all of your shares and contributions!

Let’s reverse engineer,

>>> for x in range(10)[::-1]:
	print(x)

	
9
8
7
6
5
4
3
2
1
0
>>> 

Good day . Please i don’t entirely understand this code.
i understand that the syntax for range is range(start, stop, step/stride). And that the default arguments are range(start = 0, stop=, step= 1), so what i understand from your code is:
for i in range(len(list) - 1, -1, -1):
is that the index (i) should start at len(lst) - 1 because you want to start at the index of end of the list which would be len(list) - 1 because in a list of 10 elements would be index 9 as index starts from zero.
I also understand the step/ stride portion because we want i to iterate backwards from the last index.

But what i do not understand is why the end argument is -1 which is at the end of the list. Since we are iterating backwards, shouldnt it end at the start of the list, which should be zero. That is the portion I don’t understand please can you shed more light on this?

Sorry to bother you again but i also don’t fully understand the list slice.
I know the syntax for list slicing is list[start : stop : step], i understand the step or stride portion and i know the default arguments are list[start= 0 : stop = -1 and step =1]. I understand the step part since we want to iterate backwards when using list[: : -1].
what I don’t understand , is how python knows to start at the end of the list since the default argument is still -1 or is it because 0-1=-1 so it starts from the end?
furthermore, how does it know to end at the beginning rather than at -1 which is the default argument?
Thank you very much.

the stop value uses a greater then (>) comparison, not a greater then equal or to >=. So setting 0 as stop value, then:

0 > 0 # first value is stop value. 

would be false, so we don’t get first element of list (which is at position/index 0)

Sorry sir but please I still do not understand. please can you elaborate a little bit?
also please I didn’t get and answer to this:
Sorry to bother you again but i also don’t fully understand the list slice.
I know the syntax for list slicing is list[start : stop : step], i understand the step or stride portion and i know the default arguments are list[start= 0 : stop = -1 and step =1]. I understand the step part since we want to iterate backwards when using list[: : -1].
what I don’t understand , is how python knows to start at the end of the list since the default argument is still -1 or is it because 0-1=-1 so it starts from the end?
furthermore, how does it know to end at the beginning rather than at -1 which is the default argument?
Thank you very much.

How does (stop > -1) mean the index of the beginning of the list, or are we telling python that it can stop at anywhere that is > -1?

The loop runs until the condition becomes false. That the loop could just stop randomly anywhere, would be really stupid

[::-1]

The -1 is not the stop, but the step or stride, along with the direction, to the left. So the first index of the above will be the last one in the list, and second index will be the first one in the list. Python translates this to a range()

range(len(list) - 1, -1, -1)

From the docs, referring to the slice notation list[i: j: k] = list[: : -1]

If i or j are omitted or None , they become “end” values (which end depends on the sign of *k )

Note the part in parentheses: the sign of k determines the beginning and the end of the slice.
There is a similar explanation for determining the end value for range() with a negative step.

2 Likes