Why is len(lst) > 0 necessary for solving the list challenge?

Hi, I have some question on this one.
1)Does lst[0] means that every number in the list are processed by the loop?
2) Why is the code “len(lst) > 0” crucial in the loop?

4 Likes

This could be wrong im a noob but I think this is why:

  1. lst[0] is basically selecting the 0 ‘index’ in the list = [1, 2, 3] int 1 is index 0, int 2 is index 1 etc … so when it loops after slicing the first even number

lst = lst(1:)

the lst index 0 will also change! Our lst here will become lst = [2, 3] with 0 index now being int 2. The loop will then iterate again and again until…

This … 2) len(lst) > 0 is crucial because if we don’t we enter in to an infinite loop? or something imagine if the list was all even integers once the loop has checked all the integers it has none to check which I think would create an infinite loop.

Sorry I could be a million percent wrong! But thought i’d try for my own benefit as well as yours!

Happy coding :slight_smile:

6 Likes

@ajikhan7086997408 your solution is correct, thank you!

Not quite. There will be no infinite loop, but instead an IndexError will be raised.

The purpose of checking that the length is greater than zero is to prevent attempting to access an index of an empty list–There are no indices to access.

2 Likes

@mtf but here in the last exercise, in which scenario we will attempt to access an index of an empty list .
This worked for me
#Write your function here
def delete_starting_evens(lst) :
for number in lst :
print (number)
if number % 2 == 0 :
lst = lst[1:]
else :
break
return lst

#Uncomment the lines below when your function is done
#print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))

Consider the possibility we are not accessing an empty list, but simply not running a loop on one.

@mtf Please elaborate with example

>>> for x in []:
	print (x)
else:
	print ("Empty list")

	
Empty list
>>> 

What this demonstrates is that the loop does not execute on the empty list.


Aside

Until we see how your code is indented, we cannot provide an example based upon it. I’m pointing at mere details, for the moment.


… to continue,

>>> [][1:]
[]
>>> 

This demonstrates that slice is a method of the list object. It must have its own fail check in determining the returned list, since a list is what gets returned.

>>> for x in []:
	print (x)
else:
	print ([][1:])

	
[]
>>> 

>>> print ([][0])

Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    print ([][0])
IndexError: list index out of range
>>> print ([][0:])
[]
>>> 

def delete_starting_evens(lst):
while (len(lst)>0):
if lst[0] % 2 == 0:
lst = lst[1:]
return lst

will this create a infinite loop?

Welcome, @harishramesh6788,

If the first value in the list is odd, then yes. Further, if the list contains any odd numbers, it will loop forever since the list length never changes.

Please can you expatiate?

def delete_starting_evens(lst):
  while (len(lst) > 0 and lst[0] % 2 == 0):
    lst = lst[1:]
  return lst
  1. I cant just understand why we need to check the length of the elements .
    2… lst[0] % 2 == 0):, lst = lst[1:]… the original element on indext [1] is even ie 8, so why did the function take it off. Plus, i cant find where the function says the original element on index [0] should be deleted which will then make the initial element on index [1] to be positioned at index [0] until odd number is encountered. So how come does the return value become[11, 12, 15]?
  2. Please help by breaking this down

It is important that we check that a list has some length greater than zero if we wish to access it by index. A list of length zero (the empty list) has no accessible elements so lst[0] will raise an exception.

excuse me could somebody explain this?

when this block is executed

def delete_starting_evens(lst):
  while len(lst) > 0 and lst[0]%2==0:
    lst = lst[1:]
  return lst


#Uncomment the lines below when your function is done
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))

the result is [11, 12, 15] for the first list and [] for the second list.

but when i execute this:

def delete_starting_evens(lst):
  while lst[0]%2== 0 and len(lst) > 0:
    lst = lst[1:]
  return lst


#Uncomment the lines below when your function is done
print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
print(delete_starting_evens([4, 8, 10]))

the result is still [11, 12, 15] for the first list, but i get an error on the second. Its an index error saying the list index is out of range.

The only difference between the two blocks is that on the 1st block i wrote while len(lst) > 0 and lst[0]%2==0: first, and on the 2nd i wrote while lst[0]%2== 0 and len(lst) > 0:

Thank you.

1 Like

The first operand is evaluated first, which will short-circuit if False, and thus not attempt to access an index.

The second example accesses the index first, which raises the error when the list is empty.

1 Like

I’m not sure why my answer does not work if the order of the conditions are changed from the provided solution, I get an out of index error:

def delete_starting_evens(lst):
  while (lst[0] % 2 == 0 and len(lst) > 0):
    lst = lst[1:]
  return lst

Any thoughts?

what if the list is empty:

def delete_starting_evens(lst):
  while (lst[0] % 2 == 0 and len(lst) > 0):
    lst = lst[1:]
  return lst

delete_starting_evens([])

Oh, okay. So since the first condition it doesn’t have an index of zero it will complain, so it has to fail on the len(lst)>0 first so it doesn’t get to that point?

Correct. If the first condition is False, it will short-circuit the expression and therefore not attempt the second condition which will be the one to raise an exception.