Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python code optimization

Here is a good optimization trick for many:


#INSTEAD OF:
list = []
for i in range(number):
    list.append(value)
 
#USE:
list = [value for i in range(number]


#INSTEAD OF:
for i in range(len(list)):
    #Do something
   
USE:
for i, value in enumerate(list):
    #Do something
    
    
APPLYING IT:

#THIS:
matrix = [[0 for i in range(number1)] for j in range(number2)]

#IS BETTER THAN:
matrix = []
for i in range(number1):
    row = []
    for j in range(number2):
        row.append(0)
    matrix.append(row)
 
PREVIOUS NEXT
Tagged: #python #code #optimization
ADD COMMENT
Topic
Name
3+5 =