FAQ: Code Challenge: Loops - Odd Indices

This community-built FAQ covers the “Odd Indices” exercise from the lesson “Code Challenge: Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Odd Indices

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

11 posts were split to a new topic: What is the format for range and what is an odd index?

8 posts were split to a new topic: How to use list comprehension for code challenge?

3 posts were split to a new topic: Can I use index() for the loop challenge?

4 posts were split to a new topic: Variable definition within a loop?

3 posts were merged into an existing topic: What is the format for range and what is an odd index?

2 posts were merged into an existing topic: Can I use index() for the loop challenge?

2 posts were split to a new topic: Did I loop correctly? - Code Challenge

2 posts were split to a new topic: Index out of range?

5 posts were merged into an existing topic: What is the format for range and what is an odd index?

3 posts were merged into an existing topic: What is the format for range and what is an odd index?

4 posts were split to a new topic: Using list comprehension

2 posts were split to a new topic: Code Challenge - Appending index

I THINK this would work at scale, but would like to know if this can be improved.

def odd_indices(lst):
  return [lst[i] for i in range(1, len(lst), 2)]

As stated in an earlier post, comprehensions are tough to improve upon. We can change the reading of it…

>>> def odd_indices(x):
	return [n for i, n in enumerate(x) if not i % 2]

>>> odd_indices([11,12,13,14,15,16,17,18,19])
[11, 13, 15, 17, 19]
>>> 
def odd_indices(lst):
  lit = []
  i = list(range(len(lst)))
  for n in i:
    if n%2!=0 and len(i)>0:
      lit.append(lst[n])
  return lit

I did everything so long-winded. Would this technically be more efficient, though? Instead of constantly parsing range(1, len(lst), 2) for an index, I just gave it the minimum information required.

Since you are using range, then it would be expedient to let it be the one to control the steps.

range(1, len(lst), 2)

will produce a range, [1, 3, 5, ...] so that we do not have to test the parity of the index.

for i in ...:
    result.append(lst[i])

Have you studied the list slice method, yet? That will give an instant solution with no iteration.

why my code doesn’t work

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

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

We need to check the length before attempting to access the list element in case the list is empty, whereby lst[0] will raise an exception.

>>> lst = [4, 8, 10, 11, 12, 15]
>>> lst = lst.remove(lst[0])
>>> lst
>>> 

That doesn’t look right. Are you sure you are using the .remove() method as intended?