Python List Indexing Efficiency

后端 未结 6 1244
别跟我提以往
别跟我提以往 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条回答
  •  -上瘾入骨i
    2020-12-02 00:38

    It doesn't iterate in either case. list[-1] is essentially identical to list[len(list) - 1]. A list is backed by an array, so lookups are constant time.

提交回复
热议问题