Topics

Utilizing List Elements by Iterating

Welcome to another tutorial, here you will learn how to utilize list elements in Python. In the previous tutorial, you learned how to create a list and access individual elements of that list. In continuation, you learn how to use all of the elements in a list by iterating over them, with the help of the loop functions.

Now, in cases where there are a hundred or a thousand elements in a list, and there is a need to access some or all of them in other to perform some operation or calculation, using index numbers would be very tedious to access all the elements. However, we can use the iterative method to iterate over all the elements of the list.

Introduction to Loops

Loops are used to perform redundant and repetitive tasks.

 

Using for loop

To use the for loop to iterate over a list, two things are required: a reference variable to store the element that will be iterated, and a source (i.e. list or a range()). In Python, if a for loop executes, the elements of the source are one by one copied to the reference variable for utilizing (i.e. to perform operations) them inside the look. Check out the example:

for x in range(0,5):
	print (x)

Output:

0
1
2
3
4

From the example above, x is the reference variable that each element in the range(0,5) is getting stored, iteratively (i.e. one after the other). Note, that, the rang(0,5) returns a list with elements 0 to 4 in it.

 

Using while loop

The while loop is another way of iteration. It requires just one condition to function. The while loop continues to iterate until the condition is True; so, one's it becomes false, the loop gets terminated. Check the example below:

i = 0
while i < 5:
	print (i);
	i = i+1;

Output:

0
1
2
3
4

From the above example, it is clear that the two loops returned the same output, however, the while loop, made use of the condition i<5 so it can obtain an output. in other words, while i is less than 5, printing i and keep incrementing the value of i. 

Now, do you know why we increment the value of i, after printing its value? It is because if we did not increment it, the value would have remained the same as it was declared initially, which is i =0, and the condition i<5, would have always been true and therefore, the loop will not end well.

 

Using Loops with List

Now, let’s make use of the different loops to access elements in a list. Check out the example below:

myList = ['BMW', 'Audi', 'BMW X1', 'Audi Q4']
for x in myList:
	print (x)

Output:

BMW
Audi
BMW X1
Audi Q4

 

Iterate two Lists simultaneously - zip() method

In a case where there are two lists and each element of the first list is to be added serially to each element of the second list and saved to a third empty list. This can be done by using the while loop as shown in the example below:

i = 0
A = [1, 2, 3, 4]
B = [5, 6, 7, 8]
C = []
while i < len(A):
    C.append(A[i]+B[i]);
    i=i+1

From the code, above, we added many conditions to make sure it runs without error all the time. now, what if the size of the list is not the same? Then, something different has to be done, because we need to iterate over the elements of two different lists together. You can do this with the help of the zip() function.

Now, what if the two lists are:

>>> A = [9, 8, 7, 6, 5]
>>> B = [3, 4, 5, 6, 7]

So, we can create an empty list to save the results.

>>> C = []

Afterward, the iteration process will require two reference variables, one for each list.

for x, y in zip(A, B):
	C.append(x+y)

As shown above, the zip() function can accept any number of list arguments. In our example, the two lists A and B was passed, because they both have 5 elements, as such zip() will make the loop iterate 5 times. Now, what if both lists have a different number of elements? So, if A has n elements and B has m elements, if m<n then zip() will make loop m times, else n times.

This can be done by making use of the index number of lists A and B. 

for i in range(0,5):
	C.append(A[i]+B[i])

Note that while using the above technique it is necessary to know the size of both the lists before appending, while the previous function (zip function) can be used in so many cases.