The Python enumerate() Function

The Python enumerate() Function

Omg I have found a new method to be obsessed with. 

Yayyy hooray.

Pictures of 1s and 0s. Pink, purple, blue and turquoise splodges. Text on top of splodges. Text reads: The enumerate() function in Python. Returns every item on an iterable as a tuple The first item on that tuple is the index You can choose a starting point for the index to count from
Knowledge is power

This is a phrase that has been coming up a lot for me recently.

enumerate()

Okay, so what does the enumerate() function do? The enumerate() function returns a... generator (?!)... which can then be iterated over. And when it is paired with the (for example) list() method, then it can return a list of tuples. Now, what will be in each one of these tuples?

I really do think that W3 Schools said it best...

In this case, I really do think that W3 schools said it best.
  • "The enumerate() function takes a collection (e.g. a tuple) and returns it as an enumerate object.
  • The enumerate() function adds a counter as the key of the enumerate object."
What does this look like in practise? Basically what I am trying to say is that enumerate() takes a collection - think a list, a dictionary, a tuple, an array - something that is iterable - something that is a type of data storage container - and adds like a number to every item - or like a counter - so it adds the index - and returns it as a series of tuples with every item having an index attached to it as the first item on the tuple now.

Let's have a look at this in practise.

A code example

Here is the code:

susanna_things_susanna_likes = ('Alan Turing', 'Bletchley Park', 'Margaret Elaine Hamilton')
enumerate_susanna = enumerate(susanna_things_susanna_likes)

print(list(enumerate_susanna))

And here is the output:

[(0, 'Alan Turing'), (1, 'Bletchley Park'), (2, 'Margaret Elaine Hamilton')]
So now do you see what I mean? Every item on the tuple gets turned into a tuple of its own - but it gets its own index added to it as the first item on the tuple.
But you cannot access the enumerate object directly - it's what's called a generator - it exists in potential - but it doesn't actually get saved to anything as an actual object unless you use something like for example the list() method - I'm not sure what else you could use. 
This is to protect memory.
There's no point in having a giant tuple saved if you are not gonna use all of it. And if we only need one item - then we can add in a break statement once we find it - and then we save both TIME AND MEMORY too. 
One last thing - there is an optional second parameter that you can use when calling the enumerate() method - and I LOVE optional method parameters.

Today I Also Learned...

... that it is a good option to name your optional parameters in your function calls... this is so you, another developer, or someone who is learning can understand better what the option behind the second parameter was! So for example:
This means that if I want to call the enumerate() function on the susanna_thinks_susanna_likes tuple above, but for some reason I WANT MY INDEXING TO START FROM ONE (lets say I want it to match up with some other object, or some other keys, or something), then I can add in a start parameter, (notice it is named!), and it can look something a little bit like this:
susanna_things_susanna_likes = ('Alan Turing', 'Bletchley Park', 'Margaret Elaine Hamilton')
enumerate_susanna = enumerate(susanna_things_susanna_likes, start=1)

print(list(enumerate_susanna))

Output:

[(1, 'Alan Turing'), (2, 'Bletchley Park'), (3, 'Margaret Elaine Hamilton')]
Notice how the index numbers have changed? Now would would happen if I changed 'start' to 2? And to 3?


Comments

Popular Posts