Python List: Two Ways to Iterate Over Multiple Sequences in Parallel

preview_player
Показать описание
Problem: You have multiple sequences and you would like iterate over sequences in parallel.

Solution 1: You can use python for...in loop along with range function with start value 0 and stop value would be minimum value (length of smallest sequence), otherwise you may get IndexError: list out of range error, if any sequence length is less than the value given for stop parameter.

Solution 2: In a better way you could use python built-in function zip(*iterables, strict=False), which iterate over several iterables in parallel, return tuples with an item from each one. By default, iteration stops when the shortest iterable is exhausted.

#python #pythonprogramming #pythontutorial
Рекомендации по теме