Skip to content Skip to sidebar Skip to footer

Python: List Creation By Multiplication Operator Time Complexity

Python What's the time complexity of using a = [1]*n vs. for i in range(n): a.append(1) Are both O(n) or does the first O(1)?

Solution 1:

The former is O(n), due to the use of PyList_New() with a known size. The latter is slightly worse than O(n), due to the need to resize the list after several appends.


Post a Comment for "Python: List Creation By Multiplication Operator Time Complexity"