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!