note-Python
List
Create list
1
2
3a = [0, 1, 2]
a.append(3) # a = [0, 1, 2, 3]
a += [4] # a = [0, 1, 2, 3, 4]List index
1 | for num,freq in collections.Counter(nums).items(): |
- Sort
1
intervals.sort(key=lambda x: x[0])
Random
1
random.random()
extend vs append
We see that extend is semantically clearer, and that it can run much faster than append, when you intend to append each element in an iterable to a list.If you only have a single element (not in an iterable) to add to the list, use append.
1 | import timeit |