How to traverse list in python

爱喝马黛茶的安东尼
Release: 2019-08-10 15:34:46
Original
4184 people have browsed it

How to traverse list in python

4 methods of List traversal:

Method 1:

for i in list:
    print i
Copy after login

Related recommendations: "Python Video Tutorial"

Method 2:

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

Method 3:

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

Method 4:

for i, j in enumerate(list):
    print i, j
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!