What is the iteration and iteration object in Python?

Source: Internet
Author: User
Tags iterable
In the following article, let's look at the python Iteration。 Find out what the iterations mean and what the iterations can do in Python programming.

What are the iterations of Python

Given a list or tuple, we can traverse the list or tuple through a for loop, which we call Iteration (iteration).

(in Python, the iteration is done through the for ... in)

Python's for loop is more abstract than the for loop for C because Python's for loop can be used not only on a list or a tuple, but also on other objects that can iterate .

(an object that can directly act on a for loop is called an iterative object (iterable), such as list, tuple, dict, set, str, and so on.) )

List this data type although there is subscript, but many other data types are not subscript, but as long as the Python is an iterative object , whether or not subscript, can iterate, such as dict can iterate:

>>> d = {' A ': 1, ' B ': 2, ' C ': 3}>>> for key in D: ...     Print (key) ... ACB

Because Dict storage is not ordered in list order, the resulting order of the iterations is likely to be different.

By default, the Dict iteration is key. If you want to iterate over value, you can use for value in D.values (), if you want to iterate both key and value at the same time, you can use a for-K, V in D.items ().

Because a string is also an iterative object, it can also be used for A For loop:

>>> for ch in ' ABC ': ...     Print (CH) ... Abc

So, when we use a For loop, as long as it works on an iterative object, the for loop will work, and we don't care much about whether the object is a list or another data type.

So, how can you tell if an object is an iterative object? The method is judged by the iterable type of the collections module:

>>> from Collections Import iterable>>> isinstance (' abc ', iterable) # Whether STR can iterate true>>> Isinstance ([i], iterable) # Whether the list can iterate true>>> isinstance (123, iterable) # integer can be iterated false

The last small question, what if I want to implement a Java-like subscript loop on the list? Python's built-in enumerate function can turn a list into an index-element pair so that both the index and the element itself can be iterated in the For loop:

>>> for I, value in enumerate ([' A ', ' B ', ' C ']):     ... Print (i, value) ... 0 A1 B2 C

The above for loop, which also references two variables, is common in python, such as the following code:

>>> for x, y in [(1, 1), (2, 4), (3, 9)]: ...     Print (x, y) ... 1 12 43 9

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.