天天看點

Use enumerate() and zip() in Python

In Python, <code>enumerate()</code> and <code>zip()</code> are simple and useful when iterating elements of iterable (<code>list</code>,<code>tuple</code>, etc.) in a<code>for</code> loop.

You can get the index with enumerate(), and get the elements of multiple iterables with zip().

目錄

enumerate()

for loop with enumerate()

Start index from 1 with enumerate()

zip()

Get elements from multiple lists in for loop

In the case that number of elements is different

Get a list of multiple iterable elements

Using enumerate() and zip() together

Other

itertools.product() &amp;&amp; List comprehensions

itertools.count(), itertools.cycle(), and itertools.repeat()

References

<code>enumerate(iterable, start=0)</code>

Built-in Functions - enumerate() — Python 3.8.5 documentation

Equivalent to:

Normal for loop:

While by passing an iterable object in the argument of <code>enumerate()</code>, you can get <code>index, element</code>.

As in the example above, by default the index of the <code>enumerate()</code> starts from 0.

If you want to start from another number, pass it in the second argument of the <code>enumerate()</code>.

Example starting from 1:

Of course it can start from other number

In Python, the built-in function zip() aggregates the elements from multiple iterable objects (lists, tuples, etc.). It is used when iterating multiple list elements in a for loop.

Built-in Functions - zip() — Python 3.8.5 documentation

By passing an iterable object (lists, tuples, etc.) as an argument of zip(), multiple elements can be obtained simultaneously in the for loop.

The same applies not only to two, but also to three or more.

zip() ignores the extra elements

When the number of elements of each iterable object is different, <code>zip()</code> ignores the extra elements.

itertools.zip_longest() fills in the missing elements

By <code>itertools.zip_longest()</code>, you can fill the missing elements with any values.

itertools.zip_longest() — Functions creating iterators for efficient looping — Python 3.8.5 documentation

By default it is filled with <code>None</code>.

You can specify the value to fill in the argument <code>fillvalue</code>.

The value to be filled is uniform even if there are multiple lists with insufficient elements. You can't specify different values.

If you want to fill multiple lists with unknown numbers of elements with different values, follow the procedure below.

Define the value to fill for all lists

Get maximum number of elements

Fill all lists up to maximum number of elements

Aggregate with zip()

The process of filling the list to the maximum number of elements uses initialization and concatenation of lists.

It can be a function like this:

<code>zip</code> returns an iterator (<code>zip</code> object) that contains <code>tuple</code> with the elements of multiple iterable objects.

It can be converted to a list with <code>list()</code>.

If you want to get the elements of multiple lists and indexes, you can use<code>enumerate()</code> and <code>zip()</code> together.

In this case, you need to enclose the elements of <code>zip()</code> in parentheses, like <code>for i, (a, b, ...) in enumerate(zip( ... ))</code>.

You can also receive the elements of <code>zip()</code> as a tuple.

for loop in Python (with range, enumerate, zip, etc.)

Infinite iterators in Python (itertools.count, cycle, repeat)

enumerate() in Python: Get the element and index from a list

zip() in Python: Get elements from multiple lists

個性簽名:時間會解決一切