# 我如何循环遍历一个列表？

``for(int i = 0; i < list.length(); i+=2) { // do something with list[i] and list[i + 1] }` `

Python 2

` `for i in xrange(0,10,2): print(i)` `

Python 3

` `for i in range(0,10,2): print(i)` `

` `mylist = [1,2,3,4,5,6,7,8,9,10] for i in mylist[::2]: print i, # prints 1 3 5 7 9 for i in mylist[1::2]: print i, # prints 2 4 6 8 10` `

` `from itertools import izip_longest def grouper(n, iterable, fillvalue=None): "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args)` `

` `for item1, item2 in grouper(2, l): # Do something with item1 and item2` `

` `it = iter([1,2,3,4,5,6]) for x, y in zip(it, it): print x, y` `

` `nums = range(10) for i in range(0, len(nums)-1, 2): print nums[i]` `

` `from itertools import imap def grouper(n, iterable): "grouper(3, 'ABCDEFG') --> ('A,'B','C'), ('D','E','F'), ('G',None,None)" args = [iter(iterable)] * n return imap(None, *args)` `

` `l=[1,2,3,4]` `

` `l=[(1,2),(3,4)]` `

` `for i,j in l: print i, j` `