How do I access the index itself for a list like the following?
ints = [8, 23, 45, 12, 78]
When I loop through it using a for
loop, how do I access the loop index, from 1 to 5 in this case?
How do I access the index itself for a list like the following?
ints = [8, 23, 45, 12, 78]
When I loop through it using a for
loop, how do I access the loop index, from 1 to 5 in this case?
First of all, the indexes will be from 0 to 4. Programming languages start counting from 0; don't forget that or you will come across an index out of bounds exception. All you need in the for loop is a variable counting from 0 to 4 like so:
Keep in mind that I wrote 0 to 5 because the loop stops one number before the max. :)
To get the value of an index use
If I were to iterate nums = [1,2,3,4,5] I would do
Or get the length as l = len(nums)
Old fashioned way:
List comprehension:
The fastest way to access indexes of list within loop in Python 2.7 is to use the range method for small lists and enumerate method for medium and huge size lists.
Please see different approaches which can be used to iterate over list and access index value and their performance metrics (which I suppose would be useful for you) in code samples below:
See performance metrics for each method below:
As the result, using
range
method is the fastest one up to list with 1000 items. For list with size > 10 000 itemsenumerate
is the winner.Adding some useful links below:
What is the difference between range and xrange functions in Python 2.X?
What is faster for loop using enumerate or for loop using xrange in Python?
range(len(list)) or enumerate(list)?
Use
enumerate
to get the index with the element as you iterate:And note that Python's indexes start at zero, so you would get 0 to 4 with the above. If you want the count, 1 to 5, do this:
Unidiomatic control flow
What you are asking for is the Pythonic equivalent of the following, which is the algorithm most programmers of lower-level languages would use:
Or in languages that do not have a for-each loop:
or sometimes more commonly (but unidiomatically) found in Python:
Use the Enumerate Function
Python's
enumerate
function reduces the visual clutter by hiding the accounting for the indexes, and encapsulating the iterable into another iterable (anenumerate
object) that yields a two-item tuple of the index and the item that the original iterable would provide. That looks like this:This code sample is fairly well the canonical example of the difference between code that is idiomatic of Python and code that is not. Idiomatic code is sophisticated (but not complicated) Python, written in the way that it was intended to be used. Idiomatic code is expected by the designers of the language, which means that usually this code is not just more readable, but also more efficient.
Getting a count
Even if you don't need indexes as you go, but you need a count of the iterations (sometimes desirable) you can start with
1
and the final number will be your count.The count seems to be more what you intend to ask for (as opposed to index) when you said you wanted from 1 to 5.
Breaking it down - a step by step explanation
To break these examples down, say we have a list of items that we want to iterate over with an index:
Now we pass this iterable to enumerate, creating an enumerate object:
We can pull the first item out of this iterable that we would get in a loop with the
next
function:And we see we get a tuple of
0
, the first index, and'a'
, the first item:we can use what is referred to as "sequence unpacking" to extract the elements from this two-tuple:
and when we inspect
index
, we find it refers to the first index, 0, anditem
refers to the first item,'a'
.Conclusion
So do this:
I don't know if the following is pythonic or not, but it uses the Python function
enumerate
and prints the index and the value.Output: