Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Which is faster to initialize lists?
Python offers multiple ways to initialize lists, but their performance varies significantly. Understanding which method is fastest helps write more efficient code. This article compares four common list initialization methods: for loops, while loops, list comprehensions, and the star operator.
Performance Comparison of List Initialization Methods
Let's benchmark four different approaches to initialize a list with 10,000 zeros and measure their execution times ?
import time
# Initialize lists to store execution times
for_loop_times = []
while_loop_times = []
list_comp_times = []
star_operator_times = []
# Run 500 iterations for reliable averages
for iteration in range(500):
# Method 1: For loop with append()
start = time.time()
result = []
for i in range(10000):
result.append(0)
stop = time.time()
for_loop_times.append(stop - start)
# Method 2: While loop with append()
start = time.time()
result = []
i = 0
while i < 10000:
result.append(0)
i += 1
stop = time.time()
while_loop_times.append(stop - start)
# Method 3: List comprehension
start = time.time()
result = [0 for i in range(10000)]
stop = time.time()
list_comp_times.append(stop - start)
# Method 4: Star (*) operator
start = time.time()
result = [0] * 10000
stop = time.time()
star_operator_times.append(stop - start)
# Calculate and display averages
print(f"Average time taken by for loop: {sum(for_loop_times)/500:.8f}")
print(f"Average time taken by while loop: {sum(while_loop_times)/500:.8f}")
print(f"Average time taken by list comprehension: {sum(list_comp_times)/500:.8f}")
print(f"Average time taken by * operator: {sum(star_operator_times)/500:.8f}")
Average time taken by for loop: 0.00623725 Average time taken by while loop: 0.00887670 Average time taken by list comprehension: 0.00318485 Average time taken by * operator: 0.00037154
Performance Analysis
The results show clear performance differences between the methods ?
| Method | Relative Speed | Use Case |
|---|---|---|
| Star (*) operator | Fastest (~17x faster) | Simple repetition of same value |
| List comprehension | Fast (~2x faster than for) | Complex initialization logic |
| For loop | Moderate | When you need loop control |
| While loop | Slowest | Conditional iteration |
Why the Star Operator is Fastest
The star operator ([0] * n) is implemented in C and creates the list in a single operation. List comprehensions are faster than loops because they're optimized at the bytecode level. While loops are slowest due to additional variable management overhead.
Practical Examples
When to Use Each Method
# Use star operator for simple repetition
zeros = [0] * 1000
ones = [1] * 500
# Use list comprehension for complex initialization
squares = [x**2 for x in range(10)]
even_numbers = [x for x in range(20) if x % 2 == 0]
# Use for loop when you need control or side effects
result = []
for i in range(10):
if i % 2 == 0:
result.append(i)
print(f"Added {i}")
Added 0 Added 2 Added 4 Added 6 Added 8
Conclusion
For simple value repetition, the star operator is dramatically faster. List comprehensions offer the best balance of speed and flexibility. Choose the method that best fits your specific use case and readability requirements.
