Next →

Looping Through a List

You can use a for loop to go through each item in a list.

Example:

‍fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

This prints each fruit in the list.

Looping Through a Dictionary

To loop through a dictionary’s keys and values, use .items().

Example:

student = {"name": "Alice", "age": 25}
for key, value in student.items():    
	print(key, ":", value)

This prints each key and its associated value.