sec03 - Understanding Python for-loops with Lists, Tuples, Dicts, Sets, and Strings
スポンサーリンク

Understanding the Relationship Between for Loops and Data Structures

In this lecture, we’ll explore how different data structures yield their values when iterated with a for loop. Let’s focus on “what is retrieved when you iterate over various types of data with a for loop.”

We’ll go through five data structures—list, tuple, set, dict, and string—executing each in order and observing their outputs to understand their differences.

1. Iterating Over a list

A list is an ordered collection of data. When you iterate through it with a for loop, the elements are retrieved sequentially from the first to the last.

fruits = ['apple', 'banana', 'cherry']

for fruit in fruits:
    print(fruit)

Example output:

apple
banana
cherry

As shown above, each element is retrieved one by one in order. In the for loop, the variable fruit is sequentially assigned each element from the list.

スポンサーリンク

2. Iterating Over a tuple

The way a tuple behaves in a for loop is the same as a list.

coordinates = (10, 20, 30)

for x in coordinates:
    print(x)

Example output:

10
20
30

Each value is retrieved one by one in the same way.

You can also iterate through a list that contains tuples as its elements.

points = [(1, 2), (3, 4), (5, 6)]

for point in points:
    print(point)

Example output:

(1, 2)
(3, 4)
(5, 6)

You can even unpack the tuple elements directly within the for loop.

points = [(1, 2), (3, 4), (5, 6)]

for x, y in points:
    print(f'x={x}, y={y}')

Example output:

x=1, y=2
x=3, y=4
x=5, y=6

Unpacking in a for loop is done by specifying variables separated by commas—like x, y—between for and in. Each time the loop runs, the tuple element from the list is unpacked, and its values are assigned to x and y respectively.

3. Iterating Over a set

When you iterate over a set, its elements are retrieved one by one, but the order is not guaranteed.

colors = {'red', 'green', 'blue'}

for color in colors:
    print(color)

Example output (order may vary each time you run it):

green
red
blue

Since a set is unordered, the output order may differ each time the program runs.

4. Iterating Over a dict

A dict (dictionary) is a data structure that holds “key–value pairs.” When you iterate through it with a for loop, its behavior changes depending on what you retrieve.

(1) Iterating over the dict itself → keys are retrieved

prices = {'apple': 120, 'banana': 100, 'orange': 150}

for key in prices:
    print(key)
apple
banana
orange

(2) Using .values() → values are retrieved

prices = {'apple': 120, 'banana': 100, 'orange': 150}

for value in prices.values():
    print(value)
120
100
150

(3) Using .items() → (key, value) tuples are retrieved

prices = {'apple': 120, 'banana': 100, 'orange': 150}

for item in prices.items():
    print(item)
('apple', 120)
('banana', 100)
('orange', 150)

(4) Unpacking the result of .items()

prices = {'apple': 120, 'banana': 100, 'orange': 150}

for key, value in prices.items():
    print(f'{key} → {value} yen')
apple → 120 yen
banana → 100 yen
orange → 150 yen

5. Iterating Over a string

You can also iterate over a string. In this case, each character is retrieved one by one.

word = 'Hello'

for ch in word:
    print(ch)
H
e
l
l
o

This behavior is the same for other languages as well. For example, the following code retrieves each character from a Japanese string one at a time.

greeting = 'こんにちは'

for ch in greeting:
    print(ch)
こ
ん
に
ち
は

6. Summary: Common Points and Essence of the for Loop

So far, we’ve seen how data is retrieved when iterating over lists, tuples, sets, dicts, and strings with a for loop.

Data StructureWhat Is RetrievedNotes
listElements in orderOrdered
tupleElements in orderOrdered
setElements (unordered)Unordered
dictKeys (or values/items)Can be unpacked with .items()
stringEach characterWorks with any language

Across all data structures, the for loop retrieves and processes each element one by one. This is the essence of the for loop.

スポンサーリンク