Lesson 1 - part A


Enumerate()

enumerate() is a built in function in Python that return an enumerate object.

Let's say you have a list of students. If you want to read out all the name of the students, the below algorithm will do.


for student in student list :

read out name of students



Sound easy right? But what if you want to split the students into two different groups. You can't just use the 'for' statement to do that.

You have to use a more complex algorithm for that. For example, you can do it by calculating the number of students and divide the number by two (assuming the number of student is even) . Then, allocate the students into one of the groups until the number of students in the group is equal to the number you obtained earlier on. Then allocate the remaining students into the second group.



Use a variable called 'count' that refer to the number of half of the number of students.

count = number of students/2

for student in studentlist:

if count > 0 :

put that student on group1

count=count-1

else :

put that student on group2



Basically, what this algorithm above is trying to say is that if the variable 'count' is more than 0 , we should place one of the student into group1 as there is still space in group1.

Every time we place a student into group1, we decrease the count by 1 because the number of students that group1 can accommodate had decreased by 1. Once the count is equal to zero, we will place students in group2.

Though this is a good way to divide the student into two groups computationally, there are still many other ways to do this.

This is the second way to do this. Refer one of the students as the number 0. Then refer another student as the number 1 and keep on referring all the other students with a number but each time increase that number by 1. Allocate the students who are referred by even number to group1 while the students who are referred by odd number to group2.

In Python, we can use the enumerate() function on a list to return a tuple of out each element in the list. In this case, the each of the tuples produced would contain the number referring to the student and the student name itself.

Let's say originally, we had a list of student name.

studentlist = [“Jack”, “ Mary”, “John”, “Peter”]

After using enumerate() function on the list containing students' names, we need to use the list() function in order to make a list of the tuples that we have created.

studentlist = list(enumerate(studentlist))

So, the resulting list would be

[(0,”Jack”) , (1,”Mary”), (2 ,“John”) , (3, “Peter”)]

Another thing to note is that normally, we would not start referring a student as '0'. In this case, we can start referring to the strings from 1. We can do that by

studentlist = list(enumerate(studentlist,1)

The resulting list would be

[(1,”Jack”) , (2,”Mary”), (3 ,“John”) , (4, “Peter”)]

After this, we can use a more advanced version of 'for' loop to iterate through the list.



for numberkey, student in studentlist :

if numberkey is even:

put student in group1

else :

put student in group2



As you see from the algorithm above, we use the term 'numberkey' to refer to the number in the tuple. By assorting the student based on 'numberkey', we can computationally assort the students to two different groups. See how useful the enumerate() function?




Go to Lesson 1 Part B

Back to Introduction

Back to Index