0

Which code or line is responsible for deleting Even Numbers.

def delete_start_evens(lst): While (len(lst) >0 and lst[0]%2==0): lst = lst[1:] return lst Print(delete_start_evens([4, 8, 10, 30, 11, 12, 15]) OUTPUT is 11, 12, 15. My point: Here the while Loop will run because the list given satisfy the condition stated with it, that is, list has length higher than Zero And the list start from 4 Which also satisfy the another condition which clearly shows lst[0] must be even. So, the while Loop will run. I get that this much.HOPE THAT IS CORRECT. BUT, WHAT IS LST=LST[1:] DOING HERE. AND WHICH CODE IS RESPONSIBLE TO CHECK AND DELETE EVEN ITEMS FROM THE LIST.WHY DO OUTPUT SHOWS EMPTY WHEN I PUT LST[3:] INSTEAD OF [1:].NEED CLARIFICATION.

28th Jan 2021, 3:25 PM
We Doru
We Doru - avatar
6 ответов
+ 8
Wedo Ru , the code is working like this: ▪︎ the while condition checks if lst is not empty, and if first element of lst is an even number ▪︎if while condition is true, a slice from the list is taken, but is not including the first element. this slice is stored in lst and overwriting the current content ▪︎this is running as long as first element of lst is an even number ▪︎if first element of lst is an odd number, the list will be returned from the function back to the caller
28th Jan 2021, 3:57 PM
Lothar
Lothar - avatar
+ 5
Wedo Ru , i have explaned in my last post how the code is working, so please read it carefully. here is a more deeper explanation: we start with a list: lst = [8, 11, 12, 15] - the code checks if list is not empty and if first element of list is an even number (using a modulo division like 8 % 2 == 0), which is true - after this check, a slice is done from the list starting with the index number 1, which is the second element of the list until the end of the list - lst[1:] makes a copy from the list that looks like [11, 12, 15], this will then stored in the list. so it looks like the first element is deleted. if you are not familiar with slicing, you should use the tutorial or use google. In general: if you wanted to remove all even numbers from a list, there are several algorithms that are better than the ones presented in the question. you could use: - a for loop with a if conditional and creating a new list that only contains odd numbers (or) - use a list comprehension (or) - use filter function
28th Jan 2021, 6:21 PM
Lothar
Lothar - avatar
+ 3
[3:] outputs [15] as expected ,so nothing like empty . when the number at index 0 is even (which is checked by number %2==0) ,the code lst[1:] is creating a new list object with numbers starting from index 1 to the last of list . So after first check lst is reduced to or we have a new lst [8,10,30,11,12,15] thereby removing even number ,but this code when encounters an odd number the loop breaks and you are left with [11,12,15]. And creating a new list object is quite expensive in terms of time it takes to create one .So try finding another solution or I will list one later .
28th Jan 2021, 3:59 PM
Abhay
Abhay - avatar
0
But which Code Allows it to do delete the even numbers 8 , 10 and 30. Since the function of while loops is to iterAte the list base on length And first even numbers, but which condition sAys to delete the even numbers from lst. I don't find . We cAnt sAy it simply do thAt. The two condition with while loop which identify length of loop And Even number At index 0 is fine to understand. But whAt is enabling the function to delete the first Four even numbers of the lst. There Are no statement such As delete. the index At 0 , 1 , 2 , 3.
28th Jan 2021, 5:49 PM
We Doru
We Doru - avatar
0
ThAnks. That is step wise explAnAtion. It does cleArs .
29th Jan 2021, 4:46 AM
We Doru
We Doru - avatar
0
BONUS Question- What if we choose lst=lst[0:]. Why do this don't bring Output? My view: If I put [ 0:], The while loop will check the the first element Again And AgAin, which in our CASE is the number FOUR. So, the loop iterate number four only for infinite time, in other words, it check four only Repeatedly leAding to NO MEANINGFUL OUTPUT. ARE MY VIEWS CORRECT.
29th Jan 2021, 5:30 AM
We Doru
We Doru - avatar