Python List Indexing Efficiency

后端 未结 6 1250
别跟我提以往
别跟我提以往 2020-12-01 23:35

Quick question about the built in python list object. Say you have a list with the numbers 0 - 99. You are writing a program that takes the last item in the list and uses

6条回答
  •  感情败类
    2020-12-02 00:13

    Python does not iterate through lists to find a particular index. Lists are arrays (of pointers to elements) in contiguous memory and so locating the desired element is always a simple multiplication and addition. If anything, list[-1] will be slightly slower because Python needs to add the negative index to the length to get the real index. (I doubt it is noticeably slower, however, because all that's done in C anyway.)

提交回复
热议问题