Wednesday 24 January 2024

Iterate over the elements of a list using for loop in Python

Approach 1: Iterate over the list, element by element.

 iterate_ele_by_ele.py

primes = [2, 3, 5, 7, 11]

for ele in primes:
    print(ele)

Output

2
3
5
7
11

Above snippet is a simple example of a for loop used to iterate over a list and print each element.

 

primes = [2, 3, 5, 7, 11]

This line initializes a list named primes containing five elements: the prime numbers 2, 3, 5, 7, and 11.

 

for ele in primes:

This line starts a for loop. The loop will iterate over each element in the primes list. In each iteration, the variable ele will be assigned to the current element of the list.

 

print(ele)

This line is the body of the for loop and is executed for each iteration of the loop. It prints the current value of ele, which is the element from the primes list being accessed in that iteration.

 

When this snippet is run, it sequentially accesses each element in the primes list, assigns it to ele, and then prints it.

 

Approach 2: Iterate over a list using index.

 

iterate_by_index.py

primes = [2, 3, 5, 7, 11]

for index in range(len(primes)):
    print(f'{index}. {primes[index]}')

Output

0. 2
1. 3
2. 5
3. 7
4. 11

Above snippet uses a for loop to iterate over the indices of a list and print each index along with its corresponding element.

 

for index in range(len(primes)):

This line initiates a for loop with a few key components:

 

a.   len(primes): This function call returns the length of the primes list, which is 5.

b.   range(len(primes)): range(5) generates a sequence of numbers from 0 to 4 (since range in Python is exclusive of the end value). These numbers will be used as indices to access elements in the primes list.

c.    for index in: This part sets up the for loop, where index will successively take on the values from the range(len(primes)) sequence – 0, 1, 2, 3, and 4 in each iteration.

 

print(f'{index}. {primes[index]}')

Inside the loop, this print statement is executed for each iteration. It uses an f-string (formatted string literal) to create a string that includes:

 

a.   index: The current index in the loop.

b.   primes[index]: The element in the primes list at the current index.

 

As a result, when this snippet is executed, it will print each index and its corresponding value from the primes list in the format "index. value".

 

Approach 3: Using enumerate method.

 

iterate_using_enumerate.py

primes = [2, 3, 5, 7, 11]

for index, prime_number in enumerate(primes):
    print(f"{index}. {prime_number}")

Output

0. 2
1. 3
2. 5
3. 7
4. 11

Above snippet uses a for loop combined with the enumerate function to iterate over a list, providing both the index and the value of each element in the list.

 

for index, prime_number in enumerate(primes):

This line starts a for loop with the enumerate function applied to the primes list.

 

a.   enumerate(primes): The enumerate function adds a counter to an iterable (in this case, the primes list) and returns it as an enumerate object. This object produces pairs that contain a count (from start, which defaults to 0) and the values obtained from iterating over the iterable.

b.   index, prime_number: In each iteration, enumerate returns a tuple (index, prime_number), where index is the current index of the element in the list, and prime_number is the actual element at that position in the primes list.


Previous                                                 Next                                                 Home

No comments:

Post a Comment