Python Program to Print Odd Indexed Elements of a List
In this article, we will be solving question based on very popular data type of Python i.e. List and Iteration. According to the question we have to print all the odd indexed elements of a list.
If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.
Question - Write a Python Program to Print Odd Indexed Elements of a List
Logic and Explanation
Question - Write a Python Program to Print Odd Indexed Elements of a List
Python Program to Print Odd Indexed Elements of a List - In this program we have print all the odd indexed elements of a list. For this we will run a loop from the first odd index of the list i.e. 1 till the length of list with a step of 2 i.e. the values in the loop will be 1, then 3, then 5 and so on till the length of list.
Now, once we got the odd index values, just call the element of list using the index and print it in the terminal. That's simple is the process. Here's the code for the same.
L = [2,5,9,7,6,3,8,9]
for i in range(1,len(L),2):
print(L[i])
Output -
5
7
3
9
One more way to do the same task can be iterating through all the numbers from 1 to length of list and then first checking them for odd number and then printing the element of the list at the corresponding index.
You can also check the code for this method below.
L = [2,5,9,7,6,3,8,9]
for i in range(1,len(L)):
if i%2 !=0:
print(L[i])
Note:- That not equal to sign is to be replace by != in the code.
So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.
For More Such Python Programs - Click Here
Enjoy Coding!!!
Also Read :-
Extract Phone Number Details Using Python
Post a Comment
For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.