Python

Python Enumerate : List, Tuple, String

By ayed_amira , on 03/26/2020 , updated on 09/10/2020 - 4 minutes to read
python enumerate

Python Enumerate : This function is a native Python function that allows you to have an automatic counter while looping through iterables (be it a list, a tuple, a string etc…).

In this article I will show you how to iterate on different types of python objects and retrieve the index and value of each element.

Python enumerate() Function

The enumerate() function can take up to two arguments :

enumerate(iterable, start=0)
  • iterable – An object that supports iteration.
  • start – The starting counter reading. By default, start value is equal to 0 (Optional).

Enumerate a Tuple

It is very simple to enumerate a python list. We can iterate on the index and the value of an element of the list by using a for loop as in the example below :

players = ['Kevin durant', 'James Harden', 'Lebron James','Stephen Curry']
for index, value in enumerate(players):
  print("player index  : %d and name value : %s" % (index, value))
Output:
player index  : 0 and name value : Kevin durant
player index  : 1 and name value : James Harden
player index  : 2 and name value : Lebron James
player index  : 3 and name value : Stephen Curry

In the example, we see that the enumerate function can return two values: the index and the value. You can also see that the index starts at 0. We can change this by using the start argument of the enumerate() function:

players = ['Kevin durant', 'James Harden', 'Lebron James','Stephen Curry']
for index, value in enumerate(players,1):
  print("player index  : %d and name value : %s" % (index, value))
Output:
player index  : 1 and name value : Kevin durant
player index  : 2 and name value : James Harden
player index  : 3 and name value : Lebron James
player index  : 4 and name value : Stephen Curry

We can see that the index starts at 1 instead of 0.

We can also use the function without the loop for :

print(list(enumerate(players,1)))
Output:
[(1, 'Kevin durant'), (2, 'James Harden'), (3, 'Lebron James'), (4, 'Stephen Curry')]

Enumerate a String

A string is a sequence of characters. It can be interesting in some cases to get each character of a string. Here is a small example that counts the number of vowels and consonants in a string using the enumerate() function:

str = "Pikachu"
nb_v = 0
nb_c = 0
for idx, ch in enumerate(str):
    print(ch)
    if ch in "aeiouyAEIOUY":
        nb_v = nb_v+1
    else:
        nb_c = nb_c + 1

print("Number of Vowels : {vowels}".format(vowels=nb_v))
print("Number of Consonants : {consonants}".format(consonants=nb_c))
Output:
Number of Vowels : 3
Number of Consonants : 4

Enumerate a List

A tuple is a collection that is orderly and unchanging. In Python, tuplets are written with round brackets.

Here is an example :

players = ('Kevin durant', 'James Harden', 'Lebron James','Stephen Curry')
for index, value in enumerate(players):
    print("player index  : %d and name value : %s" % (index, value))
Output:
player index  : 0 and name value : Kevin durant
player index  : 1 and name value : James Harden
player index  : 2 and name value : Lebron James
player index  : 3 and name value : Stephen Curry

The syntax for browsing a list and a tuple is very similar. Only the structure of the variable is modified.

Enumerate a List of Tuples

What to do when you have a list of tuple object? The syntax is pretty much the same as when browsing a tuple. We’ll take our example of basketball players by adding their ages to the list of tuples:

players = [('Kevin durant',31), ('James Harden',30 ), ('Lebron James',35),('Stephen Curry',32)]
for index, value in enumerate(players):
    name = value[0]
    age = value[1]
    print("player index  : %d, name  : %s  age : %d" % (index, name,age))
Output:
player index  : 0, name  : Kevin durant  age : 31
player index  : 1, name  : James Harden  age : 30
player index  : 2, name  : Lebron James  age : 35
player index  : 3, name  : Stephen Curry  age : 32

I hope this tutorial has helped you understand the enumerate() function. Leave me a comment if you have any questions about it! 🙂

Back to the Python section

ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Comments

Leave a comment

Your comment will be revised by the site if needed.