Table of contents
No headings in the article.
Have you ever needed to print out the elements of a list with odd indices in Python? It's a simple task, and can be easily accomplished using a few lines of code.
One way to do this is to use a
for loop that iterates through the list with a step of 2. This will allow us to access only the odd indices of the list:
L = [2,5,9,7,6,3,8,9] for i in range(1,len(L),2): print(L[i])
Alternatively, we can use a
for loop that iterates through all the indices of the list, and then check each index to see if it's odd. If it is, we can print out the element at that index:
L = [2,5,9,7,6,3,8,9] for i in range(1,len(L)): if i%2 !=0: print(L[i])
And that's it! With just a few lines of code, we've been able to print out the elements of a list with odd indices. Whether you're a beginner or an experienced programmer, this technique is sure to come in handy at some point in your coding journey.