Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- # Instead of this
- my_list = [1, 2, 3, 4, 5]
- squared_list = []
- start = time.time()
- for num in my_list:
- squared_num = num ** 2
- squared_list.append(squared_num)
- end = time.time()
- print(end - start)
- # Do this
- my_list = [1, 2, 3, 4, 5]
- start = time.time()
- sqúared_list = [num ** 2 for num in my_list]
- end = time.time()
- print(end - start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement