Getting Started
Sometimes you need to process two lists in parallel. The simplest method of doing
this is to use the
zip()
function. Here’s an example of the
zip()
function in
action:
for Value1, Value2 in zip(ListA, ListB):
print(Value1, '\t', Value2)
This code processes both
ListA
and
ListB
at the same time. The processing ends
when the
for
loop reaches the shortest of the two lists. In this case, you see the
following:
Orange 1
Yellow 2
Green 3
Brown 4
This is the tip of the iceberg. You see a host of iterator types used throughout the
book. The idea is to enable you to list just the items you want, rather than all the
items in a list or other data structure. Some of the iterators used in upcoming
chapters are a little more complicated than what you see here, but this is an
important start.
Do'stlaringiz bilan baham: |