Code Challenge Loops: Odd Indices

Hi,

I have been going through the loop challenges and I need some clarification on how the range function is used in the answer. I don’t understand why we have 1 and 2 at the beginning and end. And what this bit of code actually means.

https://www.codecademy.com/paths/finance-python/tracks/introduction-to-python-for-finance/modules/learn-python3-loops/lessons/python-functions-loops-cc/exercises/odd-indices

#Write your function here

def odd_indices(lst):

new_list =

for index in range(1, len(lst), 2):

new_list.append(lst[index])

return new_list

#Uncomment the line below when your function is done

print(odd_indices([4, 3, 7, 10, 11, -2]))

You consult document to see what these parameters are:

https://pynative.com/python-range-function/

which shows:

range(start, stop[, step])

so:

start = 1
stop = len(lst)
step = 2
2 Likes

Thank you I was getting confused now it seems really obvious

But why where you not able to use google to find the right documentation to answer this question yourself? Something to think about for next time :slight_smile:

because i thought this forum was there to help people that have questions…

Absolutely true :slight_smile: But learning to find answers/documentation which could help you is also a vital skill as developer :slight_smile:

I didn’t mean to discourage you from asking questions.

1 Like