Is the [0]*n runs in O(n) or O(1) in Python

Viewed 99

I was in an interveiw and the interviewer asked me: What is the fastest way to create a list of all zeros in Python with length = n. My answer was:

    l = [0 for i in range(n)]

Which is of order O(n). It was rejected as the interviewer told me the code bellow runs in O(1).

    l = [0]*n

My question is, how Python manages to create this list in O(1)?

0 Answers
Related