简体   繁体   中英

performance of list.remove(value) in Python 2.7

Suppose a list contains a list of integers, if I call list.remove(100) to remove element with value 100, wondering if it is O(n) or O(logN) ? I thought it is O(n) , but not sure if Python 2.7 list has any internal optimizations to improve performance even further. Thanks.

regards, Lin

Removing an item from a list in Python is O(n) . This is because the underlying space in memory must be "shifted over" now that an element is missing. There are other implementations of lists, such as a linked list, that you could use in Python for constant time removal, but the built in List data structure is definitively O(n)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM