Nearby lessons

52 of 108

Python - Collection For Loop

for Loop with List

If we want to execute an action for every element present in a list, we use the for loop.

Example 1 - Print All Elements of a List

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
0
1
2
3
4
5
6
7
8
9
10

Explanation

The loop visits every element of the list one by one.

Example 2 - Print Only Even Numbers

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
0
2
4
6
8
10

Explanation

The if statement filters only the even numbers.

Example 3 - Display Positive and Negative Index

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
A is available at positive index: 0 and at negative index: -3
B is available at positive index: 1 and at negative index: -2
C is available at positive index: 2 and at negative index: -1

Understanding the Program

  • len(l) returns the total number of elements in the list.
  • range(x) generates index values from 0 to x-1.
  • l[i] accesses the list element using its index.
  • i-x gives the corresponding negative index.

Important Points - List

  • The for loop processes one list element at a time.
  • Every element is visited exactly once.
  • You can use if conditions inside the loop.
  • You can iterate through indexes using range(len(list)).

List Summary

Statement Description
for x in list: Iterates through each list element.
if inside for Filters elements based on a condition.
len(list) Returns the total number of elements.
range(len(list)) Generates valid index positions.
list[i] Accesses an element using its index.

Important Notes

  • Lists are iterable objects.
  • The for loop automatically visits every element.
  • range(len(list)) is useful when index values are required.
  • Both positive and negative indexes can be displayed using a loop.

for Loop with Tuple

A tuple is also a sequence, so we can use the for loop to access each element one by one.

Example 1 - Print Tuple Elements

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
10
20
30
40
50

Example 2 - Find Sum of Tuple Elements

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Sum = 100

Important Points - Tuple

  • Tuple elements are processed one by one.
  • Tuple is immutable, so elements cannot be modified.
  • The for loop can only read tuple elements.

for Loop with Set

A set is also iterable. We can access every element using the for loop.

Since a set is unordered, the output order may change every time the program runs.

Example 1 - Print Set Elements

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Possible Output

40
10
30
20

Explanation

The order of elements in a set is not fixed.

Therefore, the output order may be different on different executions.

Example 2 - Print Squares of Set Elements

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Possible Output

4
16
36
64

Important Points - Set

  • Set elements are unordered.
  • Duplicate values are not allowed.
  • The output order may vary.

for Loop with Dictionary

A dictionary stores data as key : value pairs.

By default, the for loop iterates through dictionary keys.

Example 1 - Print Dictionary Keys

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
100
200
300

Explanation

When a dictionary is directly used inside a for loop, only the keys are returned.

Example 2 - Print Dictionary Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Sunny
Bunny
Chinny

Example 3 - Print Keys and Values

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
100 ----> Sunny
200 ----> Bunny
300 ----> Chinny

Explanation

The items() method returns both keys and values.

This is the most common way to iterate through a dictionary.

Important Points - Dictionary

  • Dictionary keys are unique.
  • Dictionary values can be duplicated.
  • The for loop iterates through dictionary keys.
Keep Going!Python - Continue Statement