+ 4
why pop isn't removing the last element from list-2. It should give [] but giving [10]
#Write your function here def delete_starting_evens(lst): for x in lst: if x % 2 == 0: lst.pop(0) 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]))
20 Answers
+ 7
Tomiwa Joseph is correct!
If you want to know the reason for output 10 then read the whole:
Meaning of pop - Return the element and then remove it
How to use?
ListName.pop(index)
Index means the location of the element you want!
It's default value is '-1' that is it will remove the last element!
List = [1,2,3]
print(List.pop())
print(List)
Output:
3
[1,2]
for loop -> It just moves on to the next PLACE and not the next number!
lst = [4,8,10]
for x in lst:
if x%2==0:
lst.pop(0)
print (lst)
Output: [10]
The for loop works only 2 times!
In the first loop:
lst = [4,8,10]
x = first Element or element at first place = 4
pop function will remove that 4
Because you have put 0 in the parameter!
In the second loop:
lst = [8,10]
x = element at second place = 10
Here x is even so the pop function will be again used!
pop is again removing the first element!
And now:
lst = [10]
x = third element which is not there in the lst!
Hence the loop breaks!
And you get [10]
+ 5
Abhishek Kumar
1. Question
[:]????
a = [1,2,3]
b = a[:]
c = a
a[0] = 0
print(a,b,c, sep="\n")
Output:
[0,2,3]
[1,2,3]
[0,2,3]
So in this for loop we are not removing any item in the lst[:] instead we are removing it from lst!
Both are different variables!
2. Question
if condition checks for the first part then the second part!
If the first part is wrong then it will not tru out the second part if you are using 'and'
For example:
a = 1
if a == 2 and a == 1/0:
print("hello")
Output: (nothing)
But it will show an error if you use or!
And even try to use this the other way round
So, there's no lst[0] in an empty list! Hence it will show an error!
We want that this condition should not be checked first, first if the length is not 0 then only check for the first element
+ 5
Abhishek Kumar Yess!!! You got it ryt!
+ 4
Abhishek Kumar Do you mean like this:
Input = [2,4,8,5,11,10,6,5]
Output = [5,11,10,6,5]
+ 4
+ 4
Advice: Don't prefer to use for loop when you want to remove elements
+ 4
Abhishek Kumar 🤗🤗🤗
+ 4
Abhishek Kumar done
+ 2
Abhishek Kumar
The same reason 12 is in the first print.
Solution:
def delete_starting_evens(lst):
for x in lst[:]:
if x % 2 == 0:
lst.remove(x)
return lst
copy the list with [:] then remove the even numbers with remove() not pop() from the original list
Hope this helps 😃😃
+ 2
Tomiwa Joseph i don't have to remove the whole even element from the list but until the first odd number is reached. For example,. In case one my function is doing well and removing 4,8 and 10 and then stops because odd 11 is reached but in second case it is removing 4 and 8 only, not 10...i don't know why...
+ 2
Namit Jain ohhh...shit..i got it where i was committing mistake. Thank you very much. Btw can you tell me any easy method to remove number upto odd is reached starting from first element in list.
+ 2
Namit Jain yeah exactly
+ 2
Namit Jain okay thanks...I'm new here..in also in programming so learning stuffs daily with the help of guys like you..and this amazing discussion plateform...get reply soon from people. Thank you again.
+ 2
Abhishek Kumar
Here is another beginner friendly code for the same purpose:
def delete_starting_evens(lst):
for x in lst[:]:
if x % 2 == 0
lst.remove(x)
else: break
return lst
print(delete_starting_evens([2,4,7,8,9]))
output: [7,8,9]
Note: Original list is returned if it starts with odd number😃
+ 2
Namit Jain this isn't working when all the elements of list are even... throwing indexError.
See this
https://code.sololearn.com/cSWWM3FcsiQx/?ref=app
+ 2
Tomiwa Joseph Namit Jain can you tell me why what's the difference between for x in lst and for x in lst[ : ]?
I understood why pop(0) is not able to remove some elements from the former because it is doing iteration but why latter one is able to remove all elements, because i guess it's also doing iteration... isn't. Please comment on this.
+ 2
Namit Jain can you tell me why the code isn't working when when are putting even condition first then length condition in the while condition statement?
+ 2
Namit Jain As per your answer i guess a=lst.copy() and a=lst[ : ] are same thing and either case will remains immutable if we mutate original lst.
+ 2
Yeah...i had just learnt about list.copy() option but was unaware of list[:] that it also makes immutable copy of list. Thank you.
+ 1
Tomiwa Joseph yeah this also working fine. I guess I'm having difficulty in understanding the execution order of lines in loop. I mean i don't understand sometimes that it should put some line with indentation or without. For example in below case should i use return with same indentation level as of if or for or of else..? Can you put some light on it....