+ 1

What is the logic behind putting lst[1:]. Why it print empty when lst[3:] is used instead of lst[1:]?

def delete_starting_evens(lst): While (len(lst) >0 and lst[0]%2==0): lst= lst[1:] return lst Print delete_starting_evens([6, 7,8,9,10,80]) Output is [9,10,80]

28th Jan 2021, 9:46 AM
We Doru
We Doru - avatar
19 Respostas
+ 5
it takes the first item in the list off. And that all depends on where that code is placed. If there is no value at lst[3:] then you can't print it.
28th Jan 2021, 9:51 AM
Slick
Slick - avatar
+ 5
It is a slicing technique in python Lst = [1,2,3,4,5,6] Syntex = Lst[start:stop:step] Example = Lst[0:3] result = [1,2,3]
28th Jan 2021, 9:51 AM
Ayush Kumar
Ayush Kumar - avatar
+ 4
Wedo Ru Where does the number 4 come from? if you print from lst[0:], the output will be [6,7,8,9,20,80] Please review list slicing to understand fully
29th Jan 2021, 5:55 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Can you ask those questions a little better? I'm still not sure what the issue is. In the while LOOP, there are 2 checks. if both checks pass, it does the code below it what are you even trying to do? That would be helpful
28th Jan 2021, 11:02 AM
Slick
Slick - avatar
+ 1
About defining Number, that is Same As putting it directly inside the print function. The list is defined AnywAy. New 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:29 AM
We Doru
We Doru - avatar
+ 1
The simplified code turns out to be: lst=[6, 7, 8, 9, 10, 80] while len(lst) > 0 and lst[0] % 2 == 0: lst = [7, 8, 9, 10, 80] print(lst) ———————————————————————————— Expected output: [7, 8, 9, 10, 80] Real output: [9, 10, 80] I think your code is wrong. Please check your code again.
30th Jan 2021, 12:04 AM
William Zeng
William Zeng - avatar
0
According to how I understand: if I put lst=lst[3:], the While Loop will not Run becAuse item number 9 which is At index 3 is not even. and so it Conflict with the Loop statement of lst[0]%2==0 which wAnts our first element to be Even, but it print output 9,10,80. WHY
28th Jan 2021, 10:16 AM
We Doru
We Doru - avatar
0
Why lst = lst[1:], why not lst[3:] Or what is the difference between putting other index in lst[]. Does it reAlly mAtter
28th Jan 2021, 10:30 AM
We Doru
We Doru - avatar
0
yes, it does. Just try indexes out. Plus, throw the code in an ide with a debugger so you can look whats happening one step at a time and understand
28th Jan 2021, 10:46 AM
Slick
Slick - avatar
0
Why lst[0]%2==0 is there Along with while function. Does it mean that the first Item in lst=lst[1:] must be even. Or it just mean that the list that we Are going to modify must contain An even number At index 0 so As to run the while loop. Secondly, Why when we put [5:] As lst= lst[5:]. Why do it return empty [ ]
28th Jan 2021, 10:58 AM
We Doru
We Doru - avatar
0
What Are the two check here.
28th Jan 2021, 11:03 AM
We Doru
We Doru - avatar
0
I'd start your python course over and take your time going through it this time
28th Jan 2021, 11:05 AM
Slick
Slick - avatar
0
I hAve seen the check or statement in while loops, but this one is Quite oppose to my understanding And logic.
28th Jan 2021, 11:05 AM
We Doru
We Doru - avatar
0
if you understand while loops, then you should get this. The condition can either be True or False, it doesnt matter what the checks are, just matters which one they end up being. Either True or False
28th Jan 2021, 11:07 AM
Slick
Slick - avatar
0
See here the loop will run becAuse the length of list is greater thank Zero And Also because first item is 6 which is even number, I cAn get the logic till here. Then comes lst= lst[ ]. Where putting different index cAn output empty where it wAs not expected.
28th Jan 2021, 11:10 AM
We Doru
We Doru - avatar
0
like [1:] would output 9,10,80 but if I put other number it would output empty. Why.
28th Jan 2021, 11:14 AM
We Doru
We Doru - avatar
0
It is Alright. I will keep looking
28th Jan 2021, 11:16 AM
We Doru
We Doru - avatar
0
Wedo Ru I tidied up your code so it now works nums = [6,7,8,9,20,80] def delete_starting_evens(lst): while (len(lst) >0) and lst[0] %2==0: lst= lst[3:] return lst print(delete_starting_evens(nums)) #Output for lst[3:] is [9,20,80] #Output for lst[1:] is [7,8,9,20,80]
28th Jan 2021, 9:18 PM
Rik Wittkopp
Rik Wittkopp - avatar
0
There WAs A mistAke mentioning Four. Alright it is 6 here. But it will output As No Output(I have checked). You CAn try yourself becAuse While Loop is running According with condition that's is to check index 0 . PleAse see there is A code inside While loop Which is lst= lst[1:]. When we put lst[0:], the output is No Output. I think you Are missing the concept. The loop will check And skip the even numbers. But since I put lst=lst[0:] the loop will keep repeating the element At index zero, i. e. 6. Let me SAY loop will skip 6 because it satisfy the condition As even item. But then due to lst=lst[0:] list will start from 6 AgAin, but the first item of this new list is 6, so loop will check it Again And these go on infinite times with the Same item At Index 0,i.e. 6.
29th Jan 2021, 8:08 AM
We Doru
We Doru - avatar