Knowledge Card - Traversal Patterns
Traversal Patterns
-
Linear Traversal: Iterate through the list sequentially.
- Example:
for item in my_list:
checks each element.
- Example:
-
Index-Based Traversal: Access elements using indices.
- Example:
for i in range(len(my_list)):
allows modifying elements during traversal.
- Example:
-
Nested Traversal: Traverse lists within lists.
- Example:
for sublist in my_list: for item in sublist:
handles 2D lists.
- Example:
-
Efficiency Considerations: Discuss when to use each pattern based on the problem.