What ways can we use to remove elements from a list in Python?

Because the slice specifies index 1 as the start. The new elements are inserted at that point but do not overwrite any of the other elements, just push them to the right. If the slice was trees[1:2] then the second element would be replaced by the inserted items.

In the context of [Delete Starting Even Numbers]

I use .pop(0) to remove the 1st value of the list, but applying it to the list [4, 8, 10] it returns [10] instead of why is that?

my code:

#Write your function here
def delete_starting_evens(lst):
if len(lst) >= 0:
for number in lst:
if lst[0] % 2 == 0:
lst.pop(0)
return lst

I am still unsure of how lst[1:] works on lists having only 1 element(index 0). Can you provide some examples please.

Given a list,

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

we access individual elements using subscript notation (brackets),

a[0]    =>  1
a[9]    =>  55

or we can use negative indices to read from the right,

a[-1]   =>  55
a[-10]  =>  1

To access more than one element in an adjacent sequence, we use slice notation inside the subscript. Any time we see a : (colon) inside the brackets we know that we are looking at a slice.

a[:]    =>  [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Note how the above subscript slice returns the entire list. This means the default value of the first position (before the colon) is index 0, and the last position (after the colon) is index 9 (plus 1), and the slice includes those indices and everything in between.

By adjusting only the first index, we move the start position to the right.

a[1:]    =>  [1, 2, 3, 5, 8, 13, 21, 34, 55]

Notice that the first element is not present in the list.

a[1:9]    =>  [1, 2, 3, 5, 8, 13, 21, 34]

Note how that last element is also not present. This explains the plus 1 in the example, index 9 (plus 1).

We could go on with more examples but I think we have answered the main question. Take the time to play with this and other examples to get a better feel for slices, in general.

1 Like

Thank you, I understood it now.

Iā€™m almost hating CODECADEMY very ugly. Iā€™m doing right now a computer science trainings and the problems that im getting the SYSTEM hasnā€™t show me yet how to work on that. How can we achieved or gain some knowledge like that? Iā€™m lost like never doing the loops and every time I tried a new problem the code is written different. For a beginner jumping into tech will be difficult learn with this path. Iā€™m lucky to do some research and learn on my own, but still is hard. I think the lesson should be distribute by problems, exercise and practice, otherwise will be a waste of time for anyone. Theres a lot of online bootcamp that people will prefer to paid because a teacher will be more interested that you learn whatever the heck youre doing. I never tried this type of code, and IDK how the school will pretend for me to do something that they havenā€™t show me yet. CRAZY.

#Write your function here
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]))

If you never show me how to combine WHILE and the word AND, plus assined to the len() inside the codeā€¦ NO GOOD.

1 Like

Question
why it canā€™t work in print(delete_starting_evens([4, 8, 10]))
the result is [10]

#Write your function here
def delete_starting_evens(lst):
new_lst =
for i in lst:
if i % 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]))

Think of a for loop as having a built in procedure. With each new iteration variable change, set a pointer to the next location in the iterable.

for i ...

  i
  |
[ 1, 3, 5, 7, 9 ]
     ^
     |
    next
  location

Now remove the current element (the one that i points to). The entire list is shifted left, so now the 3 is in the current position. However, the next location pointer is still where it was before we removed the element.

for i ...

  i
  |
[ 3, 5, 7, 9 ]
     ^
     |
    next
  location

That means the 3 is never seen.

2 Likes

Hi all. I decided to play around with this exercise, and Iā€™ve written four different functions to try to achieve the objective of the challenge.

def func1(lst):
  for i in range(len(lst)):
    if lst[0] % 2 == 0:
      lst.pop(0)
    else:
      break
  return lst

def func2(lst):
  dummy = lst
  for i in range(len(lst)):
    if lst[i] % 2 == 1:
      break
    dummy = lst[i+1:]
  return dummy

def func3(lst):
  i = 0
  while i < len(lst):
    if lst[i] % 2 == 1:
      break
    lst.pop(0)
    i += 1
  return lst

def func4(lst):
  while len(lst) > 0 and lst[0] % 2 == 0:
    lst.pop(0)
  return lst

Three of them seem to work, and one of them doesnā€™t. Two questions:

  1. Would anyone be able to tell me why func3 doesnā€™t work, specifically for the following two cases:
print(func3([4, 8, 10, 11, 12, 15, 16, 18])) #prints [12, 15, 16, 18]
print(func3([2, 4, 6, 6, 6, 8])) #prints [6, 6, 8]
  1. Iā€™m also not fully certain why func2 does work, specifically how this piece of codeā€¦
dummy = lst[i+1:]

ā€¦works with an empty list without producing an error.

Any help would be appreciated - thank you! :slight_smile:

If lst[i] is odd, then we donā€™t need to add 1 to i.

The reason it doesnā€™t raise an error is because you have a reference to the inputed list (dummy).

def delete_starting_evens(lst):
  for i, x in enumerate(lst):
    if x % 2: return lst[i:]
  return []

Youā€™ll find that slices are virtual and do not raise an index error.

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

I see - that makes sense about slices being virtual, thank you!

Could you possibly give me a hint as to why the following doesnā€™t work as expected in removing starting evens?

def func3(lst):
  i = 0
  while i < len(lst):
    if lst[i] % 2 == 1:
      break
    lst.pop(0)
    i += 1
  return lst

print(func3([4, 8, 10, 11, 12, 15, 16, 18])) #prints [12, 15, 16, 18]
print(func3([2, 4, 6, 6, 6, 8])) #prints [6, 6, 8]
1 Like

Donā€™t advance i when popping. What happens then?

1 Like

Ah thank you very much - that makes sense!

Iā€™ve just realised I donā€™t actually need a separate variable i if it stays constant at 0, so hereā€™s a slightly more concise version of the code:

def func3(lst):
  while len(lst) > 0:
    if lst[0] % 2 == 1:
      break
    lst.pop(0)
  return lst

Thanks again for your help!

1 Like

Youā€™re welcome. Personally I like the solution that doesnā€™t mutate the list but returns the virtual segment, or the empty list.

1 Like

Is there any advantage (possibly in different contexts) to using that over solutions that do mutate the input list?

The one advantage is that the original list stays in tact. Those original values may still have a use down the road. The returned list is not connected to the original in any way and it didnā€™t need mutating, just iteration (by the slice method).

1 Like

Oh wow - Iā€™m glad you mentioned that, as I was previously under the impression that the mutation only applies locally within the function but does not change the value of the input variable globally. Iā€™ve just tested it and found out otherwise.

def func1(lst):
  for i in range(len(lst)):
    if lst[0] % 2 == 0:
      lst.pop(0)
    else:
      break
  return lst

def func2(lst):
  dummy = lst
  for i in range(len(lst)):
    if lst[i] % 2 == 1:
      break
    dummy = lst[i+1:]
  return dummy

a = [2, 4, 6, 8, 1, 3, 5, 7]
print(func2(a)) #prints [1, 3, 5, 7]
print(a) #prints [2, 4, 6, 8, 1, 3, 5, 7]
print(func1(a)) #prints [1, 3, 5, 7]
print(a) #prints [1, 3, 5, 7]
1 Like

Yeah, lists and dictionaries are reference objects. We only ever access them by reference, whether in a function or not. The values are at the indices/keys of those objects.

1 Like

Hi, this is my code for Delete Starting Even Number problem, and I donā€™t get why it does not work. I just started learning how to code a few days ago, so I really appreciate any feedback you guys provide!

def delete_starting_evens(lst): 
 while len(lst)>=1:
   for i in range(len(lst)):
     if lst[i]%2==0:
       lst.pop(i)
     else:  
       break
   return lst

The error I got is

Hello, @code4381644046, and welcome to the Codecademy Forums!

Quite often, you can locate a bug by adding a temporary print statement in a strategic location that checks the values of variables. Try this:

def delete_starting_evens(lst): 
 while len(lst)>=1:
   for i in range(len(lst)):
     print(i, lst) # display values of variables
     if lst[i]%2==0:
       lst.pop(i)
     else:  
       break
   return lst

print(delete_starting_evens([4, 8, 10, 11, 12, 15]))

Output:

0 [4, 8, 10, 11, 12, 15]
1 [8, 10, 11, 12, 15]
2 [8, 11, 12, 15]
3 [8, 11, 15]
Traceback (most recent call last):
  File "/Python/glenn/del_stevens.py", line 11, in <module>
    print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
  File "/Python/glenn/del_stevens.py", line 5, in delete_starting_evens
    if lst[i]%2==0:
IndexError: list index out of range

Why was an IndexError raised?

1 Like