I have the following code:
list1 = [4,1,2,6]
for elem in sorted(list1):
#some task
I was wondering how many times does Python sort the list using the sorted()
method? Is it for each iteration or just once?
In other words is the following code more efficient?
list1 = [4,1,2,6]
list1 = sorted(list1)
for elem in list1:
#some task
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…