Home>Article>Backend Development> How to traverse list in python

How to traverse list in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼 Original
2019-08-02 13:11:02 4147browse

How to traverse list in python

4 methods of List traversal:

Method 1:

for i in list: print i

Related recommendations: "Python Video Tutorial"

Method 2:

for i in range(len(list)): print i, list[i]

Method 3:

for i in xrange(len(list)): print i, list[i]

Method 4:

for i, j in enumerate(list): print i, j

The difference between xrange and range:

range will directly generate a list object from all results.

xrange will not directly generate a list, but a generator that will return one value each time it is called.

Therefore, the loop performance of xrange is better than range, especially when the list returned is very large. But when you need to return a list, you can use range.

The above is the detailed content of How to traverse list in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What work can python do? Next article:What work can python do?