Nearby lessons

46 of 159

Python - For Loop with Collections

📌 What You Will Learn
  • Iterate over every element of a list, tuple, set and dictionary with a for loop
  • Filter list elements with an if statement inside a loop
  • Display positive and negative indices using len() and range()
  • Use d[x] to print dictionary values and items() to get both keys and values
  • Understand that a set is unordered so its output order may change

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

🐍Code Cell
1numbers = [0,1,2,3,4,5,6,7,8,9,10]
2 
3for n in numbers:
4 print(n)
Output
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

🐍Code Cell
1numbers = [0,1,2,3,4,5,6,7,8,9,10]
2 
3for n in numbers:
4 if n % 2 == 0:
5 print(n)
Output
0
2
4
6
8
10

Explanation

The if statement filters only the even numbers.

Example 3 - Display Positive and Negative Index

🐍Code Cell
1l = ["A", "B", "C"]
2 
3x = len(l)
4 
5for i in range(x):
6 print(l[i], "is available at positive index:", i,
7 "and at negative index:", i-x)
Output
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.

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

🐍Code Cell
1t = (10, 20, 30, 40, 50)
2 
3for x in t:
4 print(x)
Output
10
20
30
40
50

Example 2 - Find Sum of Tuple Elements

🐍Code Cell
1t = (10, 20, 30, 40)
2 
3total = 0
4 
5for x in t:
6 total = total + x
7 
8print("Sum =", total)
Output
Sum = 100

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

🐍Code Cell
1s = {10, 20, 30, 40}
2 
3for x in s:
4 print(x)
Output
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

🐍Code Cell
1s = {2, 4, 6, 8}
2 
3for x in s:
4 print(x * x)
Output
Possible Output

4
16
36
64

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

🐍Code Cell
1d = {
2 100: "Sunny",
3 200: "Bunny",
4 300: "Chinny"
5}
6 
7for x in d:
8 print(x)
Output
100
200
300

Explanation

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

Example 2 - Print Dictionary Values

🐍Code Cell
1d = {
2 100: "Sunny",
3 200: "Bunny",
4 300: "Chinny"
5}
6 
7for x in d:
8 print(d[x])
Output
Sunny
Bunny
Chinny

Example 3 - Print Keys and Values

🐍Code Cell
1d = {
2 100: "Sunny",
3 200: "Bunny",
4 300: "Chinny"
5}
6 
7for k, v in d.items():
8 print(k, "---->", v)
Output
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.

📝 Key Takeaways
  • A dictionary returns its keys by default when used directly in a for loop
  • The items() method returns both keys and values of a dictionary
  • Sets are unordered, so the output order may change on each execution
  • range(len(l)) generates valid index positions for a list
  • A for loop can only read tuple elements because tuples are immutable

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8