Which is the most efficient way to iterate through a list in python?

后端 未结 4 1103
暗喜
暗喜 2020-12-16 10:49

Say I have a list of items:

x = [1, 2, 3, 4, 5]

I need to perform some functions for each of these items. In a certain case, I need to retu

4条回答
  •  鱼传尺愫
    2020-12-16 11:36

    for item in list:
    

    its obviously the one with fewer function calls.

    If you want to get the index of items as you go use enumerate like this

    for pos, item in enumerate(collection):
    

提交回复
热议问题