Sitemap
Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Getting Values or Index from List through Loop in Python.

3 min readAug 15, 2021

--

In my opinion python is most easy computer language with most understandable syntax. It can be a good language for novice coders because of its readability and use of English language. Python goes beyond a basic web development. It can be used for Data Science, Machine learning, Data mining and many more.

Press enter or click to view image in full size
Image
Python computer language

Python uses same logic or concepts as other languages but there are some concepts that are not available in other programming languages e.g List, Tuple, Dictionary etc.
In this blog we will talk about Looping through these entities and getting values or index by looping through them. Lets get some overview of loop concept in python language(For Loop particularly).

For loop syntax:

Image

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). “X” is used as variable, you can use different name for this variable. This variable work same as other variables in python or other languages.

Now let’s get our main topic, getting values or index of items in List, Tuple, Dictionary etc. Sometime it’s confusing when we want to get index of the items but we got values. Let’s get out of this confusion.

Image

Think of the above example, what output will we get out this example. Yes, you are right the answer is values.
Output:

Image

In the Loop we are iterating over the values that’s why we are getting the values. What if we want to get the index of the items of List.
Look the following example:

Image

Now thing again of result. This time the result will be different. The result will be index this time. Remember that in programming language index start from zero “0”.
Output:

Image

In above example we are iterating over the length of items, that’s why we are getting index of the items starting from zero “0” to one less than the items length.

--

--

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Shahid Bahadur
Shahid Bahadur

Written by Shahid Bahadur

Software Engineer | Frontend Developer | Technical Writer

No responses yet